-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
Confirm before leaving page? #288
Comments
I believe you should be able to do that with a route pre-condition: https://github.com/ItalyPaleAle/svelte-spa-router/blob/master/Advanced%20Usage.md#route-pre-conditions |
Hi, I'm also currently trying to implement a confirm dialog with pre-conditions. A problem I came across thereby is that an empty page is loaded when the pre-conditions fail. To circumvent this, I've tried to use |
I don't believe that's possible at this time. Perhaps it could be done by creating another hook that is executed before the route is un-mounted. |
Thank you very much for your reply. You mean kinda like an I can try to implement such a functionality analogously to how pre-conditions are implemented, if you think that that would be a good addition. :) |
plus one for this. looking to have a user confirm exiting with unsaved data before navigation. reloading the existing route on failure of a route guard would cause the form state to be lost. 😞 @davidd7 did you ever find a workaround? |
The workaround that I eventually landed on for now:<script lang="ts">
import { get } from "svelte/store";
import { link, location } from "svelte-spa-router";
import { confirmNavigation, dirty } from "@/lib/stores/ui";
function checkLink(node: HTMLAnchorElement, params: any) {
node.addEventListener("click", check);
async function check(e: MouseEvent) {
if (get(dirty)) {
e.preventDefault();
e.stopImmediatePropagation();
const response = await new Promise((r) => {
confirmNavigation.set(r);
});
confirmNavigation.set(undefined);
if (response) {
dirty.set(false);
window.location.hash = node.getAttribute("href") || "";
}
}
}
const { update } = link(node, params);
return {
update,
destroy() {
node.removeEventListener("click", check);
},
};
}
</script>
<a ... use:checkLink>
<slot/>
</a> This is a global link component with a use directive that bails out on certain conditions ... it runs the svelte-spa-router link directive to maintain functionality. It then sets a writable with the resolve method of a promise. if that promise resolves to $confirmNavigation?.(true | false); Hope this helps someone looking for a workaround to this! I explored the option suggested above and realized that an exit route guard would be tricky because the hash updates independently of the route logic. so it could get messy figuring out if the outgoing route has exit conditions that fail and reverting the hash. This seems easier for now. Other considerations:
|
Hi, thank you for creating and maintaining this package!
I need to show a confirmation dialog before a user leaves a specific route, and call a function if she decides to leave. I can use
window.onbeforeunload
to handle cases where, for example, the user just navigates to a diferent site.But
beforeunload
does not fire if the user manually enters a different route within the same spa (one that is still prefixed with#
) in the address bar, or, e.g., if she presses the back button.I've tried asking for confirmation in the route's associated component
onDestroy
method, but I couldn't find a way to stop the component's destruction afterwards. Even if I raise an error, the component remains on the page but the url still changes.Is there any way of doing this with
svelte-spa-router
?The text was updated successfully, but these errors were encountered: