ItalyPaleAle / svelte-spa-router

Router for SPAs using Svelte 3
MIT License
1.53k stars 105 forks source link

[WIP] Add event beforeRouteChange to confirm whether or not we want to change routes #320

Open Cobrand opened 5 months ago

Cobrand commented 5 months ago

This is basically a shot at #204 , which is to add a function beforeRouteChange that may return true/false, if return false, prevent the history from being changed at all.

However I'm encountering a few problems, which is why it's still WIP. The biggest one:

Currently, in my implementation, beforeRouteChange is given to the Router component like so <Router {routes} {beforeRouteChange} /> But in the current codebase, push link pop are not in the component itself <script> but in the module part <script context="module". The problem is that in theory, I need to access beforeRouteChange in push to actually confirm we want to change the route But it is not possible to call something from "module" to the component itself, because there might be multiple components of the same kind. However, in the case of router, it does not make much sense to have multiple Router instantiated at the same time (or does it? does anyone use 2 routers?).

The question is simple: what can I do to fix this?

For now, this doesn't compile, but you may have a look at the code to see how it looks and if there are improvements. I'm posting this here a bit early to try to solve how to fix this issue.