sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
17.8k stars 1.78k forks source link

Redirects in "PageServerLoad" does result in 500 internal error within in a pnpm workspace if "preserveSymlinks" is used in vite.config.ts #12139

Open msprada opened 3 weeks ago

msprada commented 3 weeks ago

Describe the bug

First of all I'm not 100% sure if this "defect" is caused by Vite or Svelte Kit. But I decided to go for a issue in the svelte kit repo. If this is related to Vite and the Svelte Kit Team cannot handle it, I will definitely open the ticket or transfer the ticket to the other Repo.

I search for similar bugs within the list and for sure within the www, but I could not find any ones. Maybe it is related to this instance of Redirect Check wich was mentioned here => Redirects in load functions break when running client-side #5952

Situation:

To be able to reference the models library in the svelte application I added a symlink within the packages.json in the way pnpm wants it to be: "data-access-models": "workspace:*"

"dependencies":
 {
    "@lucia-auth/adapter-sqlite": "^3.0.1",
    "arctic": "^1.5.0",
    "better-sqlite3": "^9.5.0",
    "data-access-models": "workspace:*",
    "esm-env": "^1.0.0",
    "oslo": "^1.2.0"
},

So I can use this importing in my pages and components "page.svelte":

<script lang="ts"> import { OrderDto } from 'data-access-models'; export let data; const orders: OrderDto[] = data.orders; </script>

Without further changes with in the application this resulted in following error:

image

To get this working I had to change the vite.config extend the "resolve" part which results in following.


import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
    plugins: [sveltekit()],
    test: {
        include: ['src/**/*.{test,spec}.{js,ts}']
    },
    resolve: {
        preserveSymlinks: true
    }
});

So far so could I could use the models and the component was rendered perfectly.

Currently I'm implementing authentication and authorization using Lucia Auth.

So there is the need of redirect a user from my homepage if he/she is not logged in.

So I included following file under routes folder "page.server.ts" and implementing the PageServerLoad Method.

export const load: PageServerLoad = async (event) => {
    if (!event.locals.user) {
        console.error('redirect triggered!');
        redirect(302, "/signin");
    }
    return {
        user: event.locals.user
    };
};

If I open my homepage within login into the application the ui shows me a 500 error instead of redirecting me to the /signin route.

Within my server logs I can see following entry:

image

And if navigating to the url /signin the Signin Screen is shown without any problems.

Reproduction

Logs

No response

System Info

System:
    OS: macOS 11.7.10
    CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
    Memory: 437.52 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.10.0/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    pnpm: 8.15.4 - ~/.nvm/versions/node/v20.10.0/bin/pnpm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 123.0.6312.124
    Safari: 16.6.1

Severity

blocking all usage of SvelteKit

Additional Information

If there is anything missing within the description please let me know.