AlexxNB / tinro

Highly declarative, tiny, dependency free router for Svelte's web applications.
MIT License
678 stars 30 forks source link

Setting custom values in navigationData #98

Open drewrygh opened 2 years ago

drewrygh commented 2 years ago

I'm wondering if there is a recommended way to set custom data on a route, and access that in the router subscription handler.

Example:

<Route path="/top-secret" requiresAuthentication={true}>
   <SuperSecretRoute>
</Route>

router.subscribe((navigationData) => {
  if (navigationData.requiresAuthentication) {
     // force the user to re-authenticate
  }
}

My use case is that I would like to have certain Routes trigger an authentication check, and launch a modal if the user is unauthenticated. I read the docs and wrapping the routes in a {# if authenticated } is nice, but not sufficient. One relevant point here is that I would like to avoid using route.meta, since it looks like this only works from within the route. I know that route.meta could be used from within the Route, but it seems hacky (and error-prone) to have each route responsible for performing this type of check.

I imagine being able to pass custom navigation data would be useful in other scenarios as well — it would add quite a bit of flexibility.

Any thoughts on whether this is something Tinro should handle? Or if not, is there a recommended way to do something similar? Thanks

akvadrako commented 2 years ago

I would also like to associate extra data with each route and be able to access that data for the currently selected route. I'm currently doing it like this, but it's pretty hacky:

        <Route path='/x'>
            {@const _ = setTitle('home') }
            <Home />
        </Route>