Replies: 2 comments 6 replies
-
What are you protecting here that your server can't? To avoid repetition you can customize your fetch function. |
Beta Was this translation helpful? Give feedback.
-
I can directly relate to your position @arkmech.. spa's are an under valued pattern and a lot of things get lost in translation 😬 The whole reason this thread exists is that, in SPA mode, there isn't first‑class handle‑style hook on the client. Folks are ending up duplicating auth checks in every layout or load function, or wiring up ad‑hoc Now, if you want something that feels closer to the "global middleware" pattern (which is what I believe is being asked for here), svelte5‑router might be worth a look (I cranked this out for this very case). It gives you pre & post navigation hooks out of the box — meaning you can run your auth check before a route is allowed to resolve, trigger analytics, or other side‑effects beofre/after navigation. In practice, that means you define the guard once and every protected route respects it. It won’t replace SvelteKit’s core router, but for projects committed to I threw up a snazzy demo at https://demo.router.svelte.spa/protected which is basically: Feel free to ping me if ya have questions or concerns! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Following this: https://svelte.dev/docs/kit/single-page-apps
In my root
layout.ts
,I have
The load function here is running my auth checks (api call to separate server) using
redirect()
I know that generally if I had a SvelteKit server, I would use
hooks.server.ts
. As you don’t need to doawait parent()
is every load function nested in every nested layout.ts and page.ts.So I thought in SPA mode it would make sense to use
hooks.client.ts
, but based on docs: https://svelte.dev/docs/kit/hooks unfortunatelyhandle
is only under Server.Seems like
hooks.client.ts
is really only forhandleError
.Am I doing auth check correctly then in
layout.ts
and and everypage.ts
andlayout.ts
needs toreturn parent()
in load function?Note: I am getting some stale data on updating the session data. Can’t use Tanstack query in
layout.ts
so these auth calls can’t be invalidated on mutation operations. This session data I am referring to is being returned from my rootlayout.ts
Beta Was this translation helpful? Give feedback.
All reactions