Navigation issue with Browser Back Button Click
Issue :
- From http://localhost:3000 navigate to http://localhost:3000/p/hello using Link.
- Refresh the page.
- Click back button : It goes to http://localhost:3000 but still renders the content of http://localhost:3000/p/hello it is not loading the content of http://localhost:3000.
I am using "next": "^8.1.0".
@ninjawhocodes I can't reproduce the problem, can you post a reproduction repo and the version of your browser
Thanks @MarchWorks
https://github.com/ninjawhocodes/react-with-next
I have upload the code on above repo.
Steps to reproduce the issue :
- npm install.
- npm run dev.
- Once page is up click on "here". 4 . You will be on "http://localhost:3000/p/hello"
- Do a refresh from browser. 6 Click browser back button.
- It will go back to "http://localhost:3000" but it will still show the content of "http://localhost:3000/p/hello".


Aftet Clicking Back

@ninjawhocodes I can't reproduce the problem, can you post a reproduction repo and the version of your browser
https://github.com/zeit/next.js/issues/7091#issuecomment-485517546 I have uploaded the code on above and sharing the details in below in fresh comment. Please suggest if this issue can be fixed by doing some workaround.
@timneutkens there is an issue caused by importing css files. I will open a new issue with what I found
@timneutkens there is an issue caused by importing css files. I will open a new issue with what I found
okay so for time being could you please suggest a workaround ?
@ninjawhocodes use styled-jsx https://github.com/zeit/next.js#built-in-css-support or import an empty css file or even the same file that you have imported in your index page in all your pages
@ninjawhocodes personally I will recommend you to use styled-jsx but if you want to use css file then make sure that you are importing one in every page it can be the same or a different one it doesn't matter
@ninjawhocodes personally I will recommend you to use styled-jsx but if you want to use css file then make sure that you are importing one in every page it can be the same or a different one it doesn't matter
Yeah, I have to add style.css for each page then only it will work.
@ninjawhocodes you should also know that if you import b.css in b.js and a.css in a.js and you go from a.js page to b.js page you will have a.css loaded in b.js too
New distinct issue that occurs even without a page refresh: https://github.com/zeit/next.js/issues/9989
Every time I hit the back button on mobile the site I'm working on loads the page for a second but then the whole page goes blank, and I get the message "An unexpected error occurred."

Only happens in mobile (on both Chrome and Safari) but not on desktop.
My code:

To reproduce it you just visit the site on mobile and then hit the back button on your browser.
@zachwhitedev I have the same problem. Do you find some solution?
The same thing happens to me in production and development, the second I return to the home page, I put an error capture:

I see this is closed, I have to reopen a new issue?
I have also added shallow to the Link component, as in this example, and the error still appears:
<Link as={`/blog/${post.slug}`} href="/blog/[slug]" shallow={true}>
I have a similar issue:
https://github.com/vercel/next.js/issues/9989#issuecomment-663958815
@timneutkens Any update on this? This is a pretty massive issue. Is there an open issue to follow here at least?
@timneutkens Any update here?
I have this issue navigating between two static pages. I have to click back twice.
- Start on /home
- Click /work_orders, navigates properly to Work Orders
- Back Button once, see tab title change to "Home" but page content stays as Work Orders content
- Back Button again, see URL and page content change back to Home content
We use a mix of:
- CSS-in-JS (via Chakra-UI)
- CSS Module imports (aka
import styles from './SomeStyles.module.scss') - Global CSS imports for legacy components in _app.js
Not sure how CSS is causing the issue but wanted to share what we do.
I've tried on both v9.4.0 and v9.5.3-canary.27 and v9.5.3
We're seeing this issue too - navigating forward works OK but navigating backwards doesn't work.
Tested with Next version 9.5.1 and 9.5.3
Also using Chakra, as well as global CSS import.
Link (from /reports/individual, a very large page):
<Link href="/schedule-appointment">{...}</Link>
Clicking the link once takes me to /schedule-appointment, as expected.
/schedule-appointment:
import React, { useEffect } from "react";
import { string } from "prop-types";
import { getUserFromCtx } from "../utils/auth";
function Appointment({ email }) {
// Embed Calendly inline widget
useEffect(function addCalendly() {
const head = document.querySelector("head");
const script = document.createElement("script");
script.setAttribute(
"src",
"https://assets.calendly.com/assets/external/widget.js"
);
head.appendChild(script);
}, []); // second param causes this to only run once
const calendlyLink = "...";
return (
<div>
<div id="schedule_form">
<div
className="calendly-inline-widget"
data-url={calendlyLink + "?email=" + email}
style={{ minWidth: "320px", height: "580px" }}
/>
</div>
</div>
);
}
Appointment.propTypes = {
email: string.isRequired,
};
export async function getServerSideProps(context) {
const user = await getUserFromCtx(context);
// TODO: once calendly adds "Extra Guests" prefill,
// prefill the extra guests field with all the emails
// associated with a user's Account
return {
props: {
email: user.user.email,
},
};
}
export default Appointment;
Clicking the browser back button from schedule-appointment updates the route in the URL bar to /reports/individual but does not load /reports/individual's content.
Even weirder, if I click back twice (taking me to the page before I clicked the link, in this case /signin), it renders OK, then if I click forward once, I get the /schedule-appointment content rendered in /reports/individual!
If I refresh the page, I get the content for /reports/individual rendered as I expect.
Does anyone know if there's a workaround, even if it forces a hard reload of the page?
After some further investigation, I think this is caused by having a hash in the URL when navigating back. If I remove the hash, the navigation works correctly.