sveltejs / kit

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

redirect creates a blank page. #12170

Closed LuckyMika closed 2 weeks ago

LuckyMika commented 2 weeks ago

Describe the bug

In my project, I want users that go to /account to be redirected to /account/profile. The first solution I came up with is simply putting a

goto("/account/profile")

and that worked, but even on local server you could still briefly see the /account page contents. I then turned to using the redirect function, because I didn't want to start hacking around trying to make it render the /account/profile page and change the URL. When I tried using redirect in a components script tag, it changed the URL but displayed an error page with "500 Internal Server Error". Then I tried using redirect in a +page.ts file, but that just produced the blank white page.

Reproduction

  1. Create a SvelteKit app and create the folders src/account and src/account/profile
  2. Create server.ts in src/account and +page.svelte in src/account/profile
  3. Make a load function in server.ts that throws redirect:
    
    import { redirect } from "@sveltejs/kit"

export async function load() { throw redirect(300, "/account/profile") }

4. Add any element in `+page.ts`:
```html
<h1>This is an h1</h1>
  1. Run npm run dev & visit the dev server at /account in the browser
  2. Blank page!

Logs

No logs on either side.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
    Memory: 8.11 GB / 15.91 GB
  Binaries:
    Node: 20.12.1 - C:\Program Files\nodejs\node.EXE
    npm: 10.5.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 7.13.2 - C:\Users\mikaf\AppData\Roaming\npm\pnpm.CMD
  Browsers: {}
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.2.0
    @sveltejs/kit: ^2.0.0 => 2.5.7
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.0
    svelte: ^4.2.7 => 4.2.15
    vite: ^5.0.3 => 5.2.10

Severity

annoyance

Additional Information

I used npm for this, not pnpm. I can work around it with the hack mentioned in the description, but if there is a better way/fix let me know.

Conduitry commented 2 weeks ago

300 is not a correct HTTP status code for an automatic redirect.

LuckyMika commented 2 weeks ago

Thanks