sveltejs / kit

web development, streamlined

Home Page:https://kit.svelte.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redirect behaves strangely

vongohren opened this issue · comments

Describe the bug

Im doing the following in my code in my hooks.server.ts

if (!event.locals.session && event.url.pathname.startsWith('/app')) {
    console.log('redirecting to /auth')
    return redirect(303, '/auth')
  }

And get the following loggs out. The logs after "redirecting to /auth" is directly from the line of error itself. Where i console.log the resonse itself, and the two others are the type of response.headers and the value itself.

redirecting to /auth
Redirect { status: 303, location: '/auth' }
undefined
undefined
file:///Users/vongohren/code/mine-prosjekter/nameme-parentfolder/nameme/node_modules/@sveltejs/kit/src/exports/node/index.js:122
        for (const [key, value] of response.headers) {
                                            ^

TypeError: response.headers is not iterable
    at setResponse (file:///Users/vongohren/code/mine-prosjekter/nameme-parentfolder/nameme/node_modules/@sveltejs/kit/src/exports/node/index.js:122:38)
    at file:///Users/vongohren/code/mine-prosjekter/nameme-parentfolder/nameme/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:525:6

Node.js v18.19.0
error Command failed with exit code 1.

What am I doing wrong? Or is it a bug?

Reproduction

Try installing the versions im using and redirect in the hooks.server.ts

Logs

They are included in the error report itself

System Info

System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 77.47 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 16.2
  npmPackages:
    @sveltejs/adapter-auto: 2.0.0 => 2.0.0 
    @sveltejs/kit: 1.30.4 => 1.30.4 
    svelte: 4.2.18 => 4.2.18 
    vite: 4.5.3 => 4.5.3

Severity

blocking all usage of SvelteKit

Additional Information

No response

Well first thing first you should just call redirect instead of returning it. But regardless please provide an actual reproduction either with svelte lab, stack blitz or a git repo and please make it minimal (only the error should be present).

Thanks for the tips, I tried a repro on svelte lab, but I ended up with other strange errors.

But then I tried to remove return and do this as well, instead of returing a sync resolv.

const response = await resolve(event);
return response;

These minor changes made things work again.
The thing is that code worked fine before and then upgradring, it started failing, that makes things a bit more complicated.