-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: open prevent page leave modal on browser back button navigation #12061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: open prevent page leave modal on browser back button navigation #12061
Conversation
Would address #11964 |
e7a9244
to
4448c42
Compare
Implemented a workaround to show the "prevent leave" modal when users click the browser back button with unsaved changes. This solution addresses a limitation in Next.js App Router which doesn't provide built-in hooks for intercepting browser history navigation events. Technical details: - Creates a duplicate history entry as a buffer to prevent immediate back navigation, allowing the prevent leave modal to be displayed. - Uses history.go(-2) to properly navigate when user confirms leaving - Re-establishes buffer state when user cancels navigation Note: This implementation specifically targets the browser back button use case, which covers the vast majority of user navigation patterns. Forward button navigation is not supported by this workaround, but I think this is an acceptable trade-off considering the complexity of fully emulating the browser's history behavior within a Next.js application.
4448c42
to
4671702
Compare
This solution feels really hacky and there're so many edge cases that would lead to bugs due to the duplicated history entry. It's a rabbit hole. Would appreciate if I could get some consultation how to best fix this. |
Next 15.3 now released with an onNavigate hook that would enable us to cleanly implement this |
onNavigate is an event handler of the Link component, do you know if there's a way to intercept browser navigation too (back and forward)? |
Let me preface, I am aware that pushing an empty entry into the history stack is the only way to do this. But I am hesitant because as mentioned, it is a bit of a rabbit hole. If we were to implement a feature where document state was diffed and the modified flag was set to true and then back to false, clicking the back button would not do anything because the prev history item is going to be the same route that you are currently on. |
@JarrodMFlesch I wouldn't like to continue with this hacky solution, is there a proper way to deal with this issue with NEXT? |
I don't think it's a client side router problem. But a limitation of the browser. Right? |
Implemented a workaround to show the "prevent leave" modal when users click the browser back button with unsaved changes. This solution addresses a limitation in Next.js App Router which doesn't provide built-in hooks for intercepting browser history navigation events.
Technical details:
Note: This implementation specifically targets the browser back button use case, which covers the vast majority of user navigation patterns. Forward button navigation is not supported by this workaround, but I think this is an acceptable trade-off considering the complexity of fully emulating the browser's history behavior within a Next.js application.
Screen.Recording.2025-04-10.at.11.56.58.mov