1

I need to load some data when a route loads as the result of a back button press using NextJS. How can I tell if a route / page was loaded as a result of the user pressing the back button on the browser?

Mr. Robot
  • 1,334
  • 6
  • 27
  • 79
  • Does this answer your question? [Want to have an event handler for the browser's back button with next.js](https://stackoverflow.com/questions/61932918/want-to-have-an-event-handler-for-the-browsers-back-button-with-next-js) – juliomalves Nov 20 '21 at 12:36

1 Answers1

1

The window.history object provided by the browser won't tell you if someone used the back button. It and next/router and next/link are used for manipulating paths, not necessarily tracking history. For that, you probably need to manually track the user on your site, perhaps using cookies, window.localStorage, or something server-side.

The events exposed by next/router (such as all the routeChange* and beforeHistoryChange events) might be useful.