sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.73k stars 1.94k forks source link

`handle` in `hooks.js` is not being called for every url #3897

Closed jerrythomas closed 2 years ago

jerrythomas commented 2 years ago

Describe the bug

As per the documentation of svelte-kit handle in hooks

This function runs every time SvelteKit receives a request — whether that happens while the app is running, or during prerendering — and determines the response.

This sample repository contains a new project created using the following steps

npm init svelte@next
pnpm i

When I run the application usin pnpm dev or pnpm 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

pnpm preview

> kit-hooks@0.0.1 preview /Users/Jerry/Code/Vikalp/kit-hooks
> svelte-kit preview

  SvelteKit v1.0.0-next.267

  local:   http://localhost:3000
  network: not exposed

  Use --host to expose server to other devices on this network

handle /one

Next I clicked on each of the links (home, one, two), but there was no change in the output.

Repeated the same actions after stopping and restarting the application using `pnpm preview` and `pnpm dev` and the only time handle seems to be called is whn I explicitly refresh the browser.

````text
 pnpm dev

> kit-hooks@0.0.1 dev /Users/Jerry/Code/Vikalp/kit-hooks
> svelte-kit dev

  SvelteKit v1.0.0-next.267

  local:   http://localhost:3000
  network: not exposed

  Use --host to expose server to other devices on this network

^C%    `

```text
pnpm dev

> kit-hooks@0.0.1 dev /Users/Jerry/Code/Vikalp/kit-hooks
> svelte-kit dev

  SvelteKit v1.0.0-next.267

  local:   http://localhost:3000
  network: not exposed

  Use --host to expose server to other devices on this network

handle /two

### System Info

```shell
System:
    OS: macOS 12.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 24.65 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v14.17.0/bin/yarn
    npm: 8.1.4 - ~/.nvm/versions/node/v14.17.0/bin/npm
  Browsers:
    Chrome: 98.0.4758.80
    Firefox: 97.0
    Safari: 15.2
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17 
    @sveltejs/kit: next => 1.0.0-next.267 
    svelte: ^3.44.0 => 3.46.4

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.

pixelmund commented 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.

babichjacob commented 2 years ago

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.

christopherreay commented 2 years ago

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: image

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?

ivanjenny commented 2 years ago

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: image

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?

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.

talesmgodois commented 2 weeks ago

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.