Skip to content

Server hook is not running during preview #9481

@geodask

Description

@geodask

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp3-edge-caseSvelteKit cannot be used in an uncommon way

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions