next.js: Browser Back Button does not trigger window.onbeforeunload
In our application we have forms and want to prompt the user when they click the browser’s Back or Forward buttons to notify them they will lose their data if they continue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Although it is similar in nature to this issue that has been marked as Exploration
Expected Behavior
When user clicks back button, window.onbeforeunload function that is defined fires
Current Behavior
Browser redirects user to previous page without triggering window.onbeforeunload action
Special Notes
If I navigate directly to the page with the form by typing in the url, clicking the Back button correctly triggers window.onbeforeunload.
If I navigate away from the page with the form by typing in the url for another page, the window.onbeforeunload event fires correctly.
If I navigate to the page using links within the application, clicking the Back button does not trigger window.onbeforeunload.
I think this has something to do with they way the browser is treating our application when the user navigates within the application using Next Links. Instead of seeing each view as its own page and loading/unloading the page resources as the user navigates, it sees the entire application as just one page, thereby never loading/unloading resources and thus not triggering window.onbeforeunload.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 14
- Comments: 33 (6 by maintainers)
Thanks for that snippet @Fensterbank. Super useful.
I did notice one possible issue with it. Which I think is what prompted you to use the leaveConfirmed variable.
The function gets added as a listener to the event each time the component is rendered. So the page may end up creating hundreds of copies of that function, and calling each one of those copies when a user browses internally. And these never get cleared. Of course that might not be an issue in many use cases. But it was a problem with my particular scenario. Either way, I might suggest adding the router event listener in an event hook, just like the window
beforeunloadlistener.Here is a version with this approach, for anyone who gets to this page looking for another solution. Note, I have adapted it a bit further for my requirements.
So far, it seems to work pretty reliably.
@Nitaaq Try this snippet that will work.
any updates on this?
Following code work for me. 🚀
@ppatel221 yes. You can take this. I think we should not call or trigger
window.onbeforeunload, but a some function ofRouter@timneutkens Intercepting popstate will work when the user clicks the back button or forward button but will not work if a user clicks on a link to navigate to another page. Is it possible to have
beforeChangefunction which accepts a callback which contains awindow.confirm()and depending on the response we can decide to block the change and emitrouterChangeBlocked?I’d be interested in fixing this bug. Can I take over?
The
beforeunloadevent gets triggered when the window is about to unload its resources, it happens when user is navigating to another page via a Full Page ReloadOne of the most important difference between SPA and multip-page websites is that the window will never unload its resources when user is navigating between different “views” in a SPA. The browser never does full-page reload unless user is hitting the “refresh” button.
next.js provide some callback such as
Router.onRouteChangeStartbut it doesn’t support stopping the transition if the callback function returnsfalse, I think we should support thisSee #2236
I use this a bit differently, but the idea is clear.
Note that there is a router event for going back now: https://github.com/zeit/next.js#intercepting-popstate