Server hook is not running during `preview`
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the bug
Following a discord discussion https://discord.com/channels/457912077277855764/1087836825416114177 I wanted to open an issue as well. I have an issue with redirection inside the +hooks.server.ts. My goal is to have on this file some backend logic for checking a user session in the database and if one exists and the user tries to access login page then redirect him to the homepage.
During development (npm run dev) this works correctly. If I try to access the login page directly from the url the I am redirecting to the homepage. Similarly if I click on a link (<a href="/login">Go to login page</a>) then nothing happens because I should remain in homepage. I am noticing that on hover or on click there is a POST request for _data.json?x-sveltekit-invalidated_1 that I assume makes the hook run.
The issue is when I build and the run npm run preview. In this case he direct access (from the url) works as expected but the client side navigation does not work. No POST request for _data.json and I land on the login page. Also the same is true for building for node using adapter-node and then run with ORIGIN=http://localhost:3000 node -r dotenv/config build
Is this expected bahaviour?
Reproduction
I tried to recreate the issue in its most simple form: Stackblitz Template
Logs
No response
System Info
System:
OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
Memory: 5.08 GB / 7.44 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 19.8.1 - ~/.nvm/versions/node/v19.8.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v19.8.1/bin/yarn
npm: 9.5.1 - ~/.nvm/versions/node/v19.8.1/bin/npm
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.0.0
@sveltejs/adapter-node: ^1.2.3 => 1.2.3
@sveltejs/kit: ^1.5.0 => 1.12.0
svelte: ^3.54.0 => 3.57.0
vite: ^4.0.0 => 4.2.0
Severity
annoyance
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked StackBlitz reproduction, especially src/routes/+page.svelte, src/routes/login/+page.svelte, and src/hooks.server.ts. Run npm run dev and npm run preview, then compare client-side navigation with direct URL access; also check the adapter-node build path. Done means the server hook and redirect behave consistently during preview and adapter-node navigation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- full-stack
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100