next.js: getInitialProps not working with history (back button)?
I have an app with a single page index.js, and I parse the query parameters in getInitialProps:
static async getInitialProps ({query}) {
console.log('PAGE', query.screenId);
try {
const projectData = await ProjectLoader.loadProject(query);
return projectData;
}
catch (err) {
console.error(err);
}
};
I use <Link href={pathname, query} prefetch> tags to build my links.
The screenId property is key to finding the right page to render.
However, when I use the back button in the browser to a page without screenId set, getInitialProps doesn’t return screenId:undefined as expected, but another value (as you can see in the console log):
When I refresh the page, I get the right properties and see the correct results.
The “imaginary” screenId is actually a valid value, but I have no idea where this value comes from.
Why is this happening?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 26
- Comments: 56 (12 by maintainers)
Commits related to this issue
- Use `as` instead of `url` to determine URL newness Currently, using `as` will cause the router to think the URL is not changing in the case where you're re-rendering the same page with a different ro... — committed to gcpantazis/next.js by deleted user 9 years ago
- Use `as` instead of `url` to determine URL newness Currently, using `as` will cause the router to think the URL is not changing in the case where you're re-rendering the same page with a different ro... — committed to gcpantazis/next.js by deleted user 9 years ago
- Use `as` instead of `url` to determine URL newness (#4153) Currently, using `as` will cause the router to think the URL is not changing in the case where you're re-rendering the same page with a diff... — committed to vercel/next.js by gcpantazis 8 years ago

Why is this closed ? 😦
Hi,
I tried this and It worked for me. Let me know if this is fixing your issue.
You have to put Router.beforePopState() inside in cyclehook method componentDidMount() in _app.js file. (I’m using next@7.0.2) The idea is to force the browser to reload the right URL when hitting browser’s back button.
Issue in Safari still
Upgrading to 9.3.0 and changing getIntialProps to getServerSideProps solved the problem.
Getting issue in Chrome and Next 9.2.1.
getInitialPropsdoes not refetch data when using the back buttonIf you are experiencing issue that
getInitialPropsnot invoked when user click back between same pages you can use next code (somewhere during bootstrap on frontend):Is there a workaround that does NOT cause SSR? We‘d like to keep unnecessary SSR low because of backend resources.
This hack works for now, but please let me know if anyone has a good solution or cause for this problem.
just encountered this. this should not be closed
I think our issue is that we use one central dispatcher (
pages/dispatcher.js). The dispatcher decides which components to load, based on the url (we’re loading data from an API behind the scenes). This works perfectly until we start hitting the browser’s “back” button. Then you’re redirected to a random page. It seems like the issue is that our links look like this:The
hrefis always set to/dispatcheron all pages.I’ve looked a little into other issues, and it seemed like the solution mentioned here would solve it. In short the idea is to append a unique query string, which would then make
hreflook like something like this:/dispatcher?pretty=url1. Unfortunately that triggers another problem - the links just stop working (clicking them does nothing). I’ve isolated the issue by taking the code from https://github.com/zeit/next.js/tree/master/examples/parameterized-routing and making it run on one file only:Clicking any of the three links does nothing. When the query strings are removed from the links (so
hrefonly contains/test) it works fine again - unless the history part. A funny observation is that when after removing them you add them back in, and the component is hot reloaded, both links and history work - until you refresh the page.I hope anyone’s had similar issues and/or can point us into a direction for solving this.
This issue is in Firefox, Chrome when using Next 9.1.6. This needs to be reopened.
having this issue with
^8.0.0this work around appears to work but it does a full page reload which isn’t ideal, any other solutions?
Do we have any solution for this ?
My case :
I am using “next”: “^8.1.0”.
^ The above worked for me, however, it looks like it renders the page twice.
Better than it not rendering at all I guess.
Note*: This “back button” issue was only happening to me in Safari (next@7.0.2).
Hate to admit it, but I think Safari is the new IE6 🤦♂️
Anyways, good find @johanleroch !
Ok I finally could reproduce the issue.
The issue is only existent in the browsers Safari & Firefox. These browsers use a feature called “bfcache” (back-forward cache). This means the html document is cached in memory. As soon as a user uses a back button coming from an external page, no request is made on the server. Instead the html document is rendered from the cache. Since
getInitialPropsalready ran (before it was cached) it won’t run again.@timneutkens Do you have an idea about how to solve this and get all browsers inline?
Also experiencing this with Safari. Chrome works as expected.
If anything the different behaviors in the two browsers should be treated as an issue in and of itself (ie: The point @HaNdTriX made about bfcache)
So, because nobody answered, neither here nor on Slack, we solved this issue by creating a custom version of
link.js. I think all we did was to change https://github.com/zeit/next.js/blob/canary/lib/link.js#L75 toRouter[changeMethod](href + '?uri=' + as, as, { shallow }). Feels a bit hacky, but it works 😃This may not be the solution to the above problem, but the last few days I had a big “a-ha” moment with the
<Link>component when you’re using custom routing:You need both
hrefandasproperties, they both need to be valid, and they will most likely to be different from each other(!).Example:
<Link href={'/mynextjspage?articleId=' + articleId} as={'/articles/' + articleId}>aswill be the link as you want it to appear, whilehrefwill be a reference to a Page component in the/pagesfolder with parameters added as a query string.@liweinan0423 @anselmdk Are you using custom routing?
I am having the same issue…