Skip to content

set-cookie header is ignored by fetch() inside load function #7467

@Nickersoft

Description

@Nickersoft

Describe the bug

Hey folks,

Not sure if this is by design or not, but a rather annoying issue I've encountered is that all set-cookie headers returned from the fetch method inside load() are ignored and do not store a cookie on the browser. When I use the non-load fetch (global fetch) inside my +layout.ts file, it works perfectly.

However, I'm currently using URQL and its authExchange feature to make authenticated requests to my API, then refresh the access token when it expires. Unfortunately:

  • Using the fetch inside load() makes the credentialed request correctly, but fails to set the new access token cookie when a response is returned
  • Using the global fetch can't make any credentialed request without manually passing in the cookies, yet it will assign the new access token cookie correctly if it receives a response

I would try passing my cookies in as layout data from the +layout.server.ts to the +layout.ts and then pass a custom fetch function to URQL that manually assigns the cookies, but I'm not sure if this could lead to stale cookies (I don't know when the +layout.server.ts's load() function would run again).

I've also tried using hooks.server.ts to manually assign the cookies from set-cookie to the event object, but I think event must be a copy or immutable because it doesn't make a difference.

Hopefully I'm making sense. I know it's a bit confusing! I've scoured the docs and the issues here and can't find anything to point me in the right direction.

Reproduction

I created a small reproduction on the nickersoft/set-cookie branch of this repo. Once you install deps with (P)NPM, just run node server.js in one terminal, and then pnpm dev in another. After opening the site, if you check the developer tools you'll see no cookie is set, however in the terminal the response is logged, which includes a set-cookie header that attempts to set an access_token cookie to hello_world.

Logs

No response

System Info

System:
    OS: macOS 12.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 101.94 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.9.0 - ~/.asdf/installs/nodejs/18.9.0/bin/node
    npm: 8.19.1 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Brave Browser: 107.1.45.116
    Safari: 15.4
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.86 
    @sveltejs/kit: next => 1.0.0-next.531 
    svelte: ^3.52.0 => 3.52.0 
    vite: ^3.2.2 => 3.2.2

Severity

serious, but I can work around it

Additional Information

The only current way around this is not trying to server load my GraphQL queries in my app. It causes the layout to jump once it loads data, which is unfortunately not a great UX.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions