Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LimChorngUan
Copy link
Contributor

@LimChorngUan LimChorngUan commented Apr 9, 2025

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.

Screen.Recording.2025-04-10.at.11.56.58.mov

@LimChorngUan LimChorngUan changed the title feat: Open prevent page leave modal on browser back button navigation feat: open prevent page leave modal on browser back button navigation Apr 9, 2025
@cideM
Copy link

cideM commented Apr 9, 2025

Would address #11964

@LimChorngUan LimChorngUan force-pushed the prevent-leave-on-back-nav branch from e7a9244 to 4448c42 Compare April 10, 2025 09:02
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.
@LimChorngUan LimChorngUan force-pushed the prevent-leave-on-back-nav branch from 4448c42 to 4671702 Compare April 10, 2025 09:56
@LimChorngUan
Copy link
Contributor Author

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.

@paulpopus
Copy link
Contributor

Next 15.3 now released with an onNavigate hook that would enable us to cleanly implement this

@LimChorngUan
Copy link
Contributor Author

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants