How to prevent browser back and forward buttons navigation disabled in Nextjs?
Unanswered
Mini Satin posted this in #help-forum
Mini SatinOP
How to prevent browser back and forward buttons navigation disabled in Nextjs?
9 Replies
Mini SatinOP
It's requirement for me
Dunker
as i know you cant
@Mini Satin 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 ()
Mini SatinOP
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"
@Mini Satin 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