kit: Can't set cookies/headers inside `handle` hook after call to `resolve`

Describe the bug

handle hook doesn’t add cookies as expected

Reproduction

https://stackblitz.com/edit/node-shcasv

step 1: create a skeleton project step 2: add hooks.server.js:

/** @type {import("@sveltejs/kit").Handle} */
export async function handle({ event, resolve }) {
    console.log('hook ran');
    event.cookies.set('some', 'value', { secure: false, httpOnly: false });
    return await resolve(event);
}

step 3: open the site step 4: try deleting the cookie (if present) on the browser and refresh. no new cookie is added

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
    Memory: 2.05 GB / 3.73 GB
    Container: Yes
    Shell: 3.5.0 - /usr/bin/fish
  Binaries:
    Node: 18.7.0 - ~/.local/share/nvm/v18.7.0/bin/node
    Yarn: 1.22.10 - /mnt/c/Users/_/AppData/Roaming/npm/yarn
    npm: 8.15.0 - ~/.local/share/nvm/v18.7.0/bin/npm
  Browsers:
    Chrome: 103.0.5060.114
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.72 
    @sveltejs/kit: next => 1.0.0-next.480 
    svelte: ^3.44.0 => 3.50.1 
    vite: ^3.1.0 => 3.1.0

Severity

blocking all usage of SvelteKit

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 24 (15 by maintainers)

Most upvoted comments

It certainly needs to be called upon resolve because people could rely on the headers already being present on the response object when resolve returns. I’m not sure if it’s “works as designed” and needs to be documented as a caveat or if we should apply any additional cookies/headers that were set after resolve. Reopening and changing the title accordingly until we decide that.