Closed jerrythomas closed 2 years ago
Handle runs only at the server-side, if you navigate through your app after it's loaded everything is handled client side so your handle function is only called on fetch requests, initial loads and hard refresh.
handle function seemed to be the best place to implement any route protection by appropriately redirecting requests which are not allowed
They're not. Use load
functions to send (un)authorized visitors to a different page, especially based on the session
parameter and using a function to deduplicate between pages if cumbersome.
As mentioned, client-side navigations (that don't involve endpoints or any non-asset requests to the SvelteKit server) won't trigger the handle
hook, so I'm closing this for being expected behavior.
Hi, so my current app, which is built on the latest version of SvelteKit, is not calling hooks/handle
, even though it is calling __data.json for a page endpoint. Is this expected behaviour?
here is the network confirmation:
I can confirm (ive checkout about ten times), that the handle hook is not being called. Is this expected? Or not? tyty
In the documentation https://kit.svelte.dev/docs/hooks it says:
This function runs every time the SvelteKit server receives a request — whether that happens while the app is running, or during prerendering — and determines the response. It receives an event object representing the request and a function called resolve, which invokes SvelteKit's router and generates a response (rendering a page, or invoking an endpoint)
So in my app, when it is running as npm run dev
it calls the handle hook, but in npm run preview
it doesnt not. This is for a page endpoint
. It seems that other endpoints behave as expected
So Im looking at the source code: https://github.com/sveltejs/kit/blob/32a700a204e108a1fdceeffc7727bdb340335a3e/packages/kit/src/runtime/server/index.js#L168
and it looks pretty clear that is_data_request happens during the resolve phase of the hook processing. So I am unable to understand how it is possible that my endpoint is being called without the hook being called. I mean.. there are clear just console.log statements in the hook, and no output is being printed from them.
As I mentioned, this works as expected for the npm run dev
exectuion, but not the npm run preview
after a build. Perhaps there is something in the build process that is making this possible?
Hi, so my current app, which is built on the latest version of SvelteKit, is not calling
hooks/handle
, even though it is calling __data.json for a page endpoint. Is this expected behaviour?here is the network confirmation:
I can confirm (ive checkout about ten times), that the handle hook is not being called. Is this expected? Or not? tyty
In the documentation https://kit.svelte.dev/docs/hooks it says:
This function runs every time the SvelteKit server receives a request — whether that happens while the app is running, or during prerendering — and determines the response. It receives an event object representing the request and a function called resolve, which invokes SvelteKit's router and generates a response (rendering a page, or invoking an endpoint)
So in my app, when it is running as
npm run dev
it calls the handle hook, but innpm run preview
it doesnt not. This is for apage endpoint
. It seems that other endpoints behave as expectedSo Im looking at the source code:
and it looks pretty clear that is_data_request happens during the resolve phase of the hook processing. So I am unable to understand how it is possible that my endpoint is being called without the hook being called. I mean.. there are clear just console.log statements in the hook, and no output is being printed from them.
As I mentioned, this works as expected for the
npm run dev
exectuion, but not thenpm run preview
after a build. Perhaps there is something in the build process that is making this possible?
Did you manage to find a reason for this? I'm new to SvelteKit and think I am experiencing the same issue. Works fine in dev but not after being built.
Cookie authorization and role recognition has been very tough with Sveltekit.
I still don't know if the recommended way is to handle that on the hooks or on the load funtion. I should be able to this on a single place. Not create a page.client or page.server for each page.
Describe the bug
As per the documentation of svelte-kit handle in hooks
This sample repository contains a new project created using the following steps
When I run the application usin
pnpm dev
orpnpm build && pnpm preview
. The output of the logged calls is not consistent with the pages visited.Reproduction
https://github.com/jerrythomas/svelte-kit-hook-issue
Logs
Severity
blocking an upgrade
Additional Information
I was working on an auth + role based route guard and handle function seemed to be the best place to implement any route protection by appropriately redirecting requests which are not allowed. If the handle function received a call for every route page/endpoint this would be possible, but it does not work as it used to.