How to prevent browser back and forward buttons navigation disabled in Nextjs?
Unanswered
Flemish Giant posted this in #help-forum
Flemish GiantOP
How to prevent browser back and forward buttons navigation disabled in Nextjs?
9 Replies
Flemish GiantOP
It's requirement for me
Dunker
as i know you cant
@Flemish Giant It's requirement for me
Honestly, wouldn't recommend it, but since you really want to, the only way i can think of is using:
https://nextjs.org/docs/app/api-reference/functions/use-router#router-events
https://nextjs.org/docs/app/api-reference/functions/use-router#router-events
Inside that useEffect you can check for page url changes and then negate the effect, maybe using something like router.replace ()
Flemish GiantOP
In my case if the user once logged in after can't back the login page again.
Dunker
show at login page, "you already logged in"
@Flemish Giant In my case if the user once logged in after can't back the login page again.
Then you shouldn't be "preventing browser back and forward buttons"
Use a middleware to do the checking and redirecting instead.
Dont try to negate/disable it outright.
https://nextjs.org/docs/app/building-your-application/routing/middleware
Dont try to negate/disable it outright.
https://nextjs.org/docs/app/building-your-application/routing/middleware