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?
Asked
Active
Viewed 2,188 times
1 Answers
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.

Cameron Pittman
- 41
- 3