$middleware.svelte to run code on client-side each time child page is rendered
See original GitHub issue$ layout.svelte
is a great idea to reflect the visual aspects and components common to all child pages.
It would be interesting if there was also a $ middleware.svelte
with functions and routines that could be executed whenever a descendant page was called to be displayed, for example, because of access rules, filters, user functions or things like that, maybe more in terms of security.
I’m starting to work with sveltekit and I miss it.
I still haven’t found a way to do that in Sveltekit.
I would be grateful if someone clarified to me if it already exists or if there is a way to do it safely.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:21 (8 by maintainers)
Top Results From Across the Web
Modules • Docs • SvelteKit
A subdirectory of $lib . SvelteKit will prevent you from importing any modules in $lib/server into client-side code. See server-only modules. $service ...
Read more >'Sapper: Towards the ideal web app framework' - Svelte
Finding the code responsible for a given page is easy, because you can just look at the filesystem rather than playing 'guess the...
Read more >Page options • Docs • SvelteKit
By default, SvelteKit will render (or prerender) any component first on the server and send it to the client as HTML. It will...
Read more >Docs • Sapper
Each page of your app is a Svelte component; You create pages by adding files to the src/routes directory of your project. These...
Read more >FAQ • SvelteKit
You can execute any one-time setup code in hooks.js and import your database helpers into any endpoint that needs them. How do I...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
load
will re-run whenever its dependencies change. For example, this function……will re-run whenever
session
changes . That’s generally the place you want to do these sorts of checks.Triggering a change to
session
without a page reload is up to the developer; for example you could do this sort of thing……but that doesn’t solve the problem of e.g. logging out in a different tab. It’s solvable in userland, but I almost wonder if SvelteKit should have some built-in stuff for refreshing the session from the server periodically (including when you refocus a page, swr style). #46 included some thinking along those lines.
@moisesbites You can get
load
to run everytime, even within a$layout
page for all sub-pages, by adding the following to your load function:I’m doing the same thing where I
verify
the JWT everytime the user changes routes under the/private
route in my app. Here is my full load function:I think this has something to do with the fact that
load
now depends onpage
, which changes every time the router changes, so it causes it to trigger. But I’m not exactly sure what’s going on under the hood.