-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
onNavigate lifecycle function #560
Comments
We need a hook onBeforeNavigate as well. As referenced by @benmccann in sapper#1047, it's very useful to be able to cancel navigation when a user is editing something and has forgotten to click "Save". |
If this will not be handled by svelte/kit, I will provide a package for it, simliar to this. One question is, should |
@PatrickG It would make sense to me if the router check if |
I didn't mean it that way. I mean, in sapper-navigation-enhancer, I have this: beforeNavigate(callback: () => any, useBeforeUnload: boolean): () => void; When Btw, maybe we should create another issue for this. Because it is not |
Another benefit of an onBeforeNavigate() {
return { previousUrl: location.href }
} and access this object on the next route with edit: just realized by looking at the source, that returning the state should not be needed as the current state gets already injected in the router 🎉 onBeforeNavigate() {
history.state = { ...history.state, previousUrl: location.href }
} |
Hey has there been any new progress on this? I just now wanted to add this to my app but as it seems not possible, I resulted to making this very ugly hack: https://gist.github.com/TeemuKoivisto/a22ba96d4eb52b95776202d5038815b7 Which seems to prevent most cases. Well, I couldn't bother fixing that forward backspacing but I just want to move on to another things. Good enough for me I guess. And well the onclicks on those links could be intercepted better. |
Additionally, It would be nice if the onBeforeNavigate adds the ability to intercept a navigation intent and cancel it if needed for features like "Confirm that you want to leave" modals. I've created a separate request for the |
Don't know about the onBeforeNavigate, but I just made a simple computed function for onNavigation like this: import { page } from "$app/stores";
let pageLog: string = $page.path;
$: if (pageLog !== $page.path) {
pageLog = $page.path;
// On navigation logic goes here
} This checks if the URL has changed and if yes, executes a function. @TeemuKoivisto Maybe this works cleaner for you. P.S. I absolutely love |
Another syntax idea after skimming #2982. It would work very similiarly to vue router: onNavigate(({ from, to, next }) => {
// before route
next() // confirm route
// after route
})
|
#552 suggested a need for a lifecycle function that gets called when navigation occurs but the destination component is the same as the one we're already on (since
onMount
only runs the first time the component is mounted, and the alternative — unmounting components upon navigation — is bad for a variety of reasons).I propose an
onNavigate
hook that lives in$app/navigation
and looks something like this:As mentioned in #404 (comment):
The text was updated successfully, but these errors were encountered: