Add event hook for cancelable browser back and forward navigation #1648
Replies: 4 comments
-
Correct me if I'm wrong, but cancelling the history navigation is impossible, otherwise it would be incredibly easy to trap someone on your website. There is the |
Beta Was this translation helpful? Give feedback.
-
You're right, but according to this 'very reliable' source, pushing the same state back should work: https://chat.openai.com/share/00d4058e-bbd1-46eb-8543-95a436bf613f At least I'd like to be able to cancel the functions Inertia executes with this event to implement my own hack. Normally, the browser would dispatch an unload event when using normal navigation. So, I think it should be possible to replicate the same functionality. I'm building an editor with react flow and if the user moves left, the native back even in the browser is sometimes triggered by accident and the complete state is gone :/ |
Beta Was this translation helpful? Give feedback.
-
Hey! Thanks for sharing this idea. Since it's a feature request and not an issue, I've moved it to the discussions "ideas" category. I've actually wanted to solve this exact problem myself in the past, but wasn't able to come up with an elegant solution. The issue is that the browser does not let you prevent Looking around tonight to see how others handle this, I learned that Vue Router actually does support this — but it's doing some pretty clever hackery to make it actually work. They allow you to prevent navigation using the It's actually more complicated than that, because it's possible to go back multiple steps in history by pressing and holding down the back button and choosing a specific point in your history. What they do to solve that is keep track of each page history in local state and then calculate the delta between where you were in history state and where you've gone to, and then they call This seems to work, although the Vue Router code has lots of little handlers for weird gotchas, as this isn't really how browsers intended for this API to be used. This is something we could potentially try and implement in the future, but it's a non-trivial thing to implement, so I don't think it's going to happen soon, just being honest. |
Beta Was this translation helpful? Give feedback.
-
I understand this is a tougher problem to solve. However, with inertia navigation I'm losing access to the |
Beta Was this translation helpful? Give feedback.
-
Versions:
@inertiajs/core
version: 1.0.10Describe the problem:
Currently, Inertia does not provide a specific event hook for intercepting and canceling browser back and forward navigation. While the library allows capturing in-page navigation, handling navigation through the browser's back and forward buttons is not directly supported. This limitation poses challenges for scenarios such as warning users about unsaved changes, as well as other contexts where controlling or canceling back and forward navigation is necessary.
The expected behavior would be to have a way within Inertia to easily detect and optionally cancel back and forward navigation, possibly through specific events or hooks designed for this purpose.
Steps to reproduce:
The absence of an event hook or mechanism within Inertia to handle browser back and forward navigation necessitates workarounds, such as using the
popstate
event, which may interfere with other client-side routing logic.My preferred implementation
Beta Was this translation helpful? Give feedback.
All reactions