Inconsistent behaviour when changing headers of generated response
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- javascript
Research direction
Start with packages/kit/src/runtime/server/respond.js at the cited lines, then run the linked reproduction repository. Inspect src/routes/a/b/+page.server.ts and src/routes/a/+layout.server.ts to compare event.setHeaders and event.cookies.set after the redirect. Done means their post-response behavior is consistent with the intended contract.
Written by the indexing model from the issue text.
Description
Describe the bug
When setting headers of an already generated response through event.setHeaders and event.cookies.set the behaviour is inconsistent.
event.cookies.set throws an error, while event.setHeaders does not. Based on the code at https://github.com/sveltejs/kit/blob/c749e85d2502d5eac03f739c8f18a2f3a4dcb13e/packages/kit/src/runtime/server/respond.js#L541-L547 I would assume they are both supposed to fail.
Server logs when going to http://localhost:5173/a/b in the reproduction repo I've attached shows the issue:
starting /a/b
redirect
resolved /a/b
would error layout event.setHeaders: false
would error layout event.cookies.set: true
Since there is no indirection in front of these functions destructuring like the following, would prevent event.cookies.set from erroring as well:
export const load: LayoutServerLoad = async ({ setHeaders, cookies: { set }}) => {
Additionally, I would suggest adding some mechanism for checking this state, besides checking the toString of event.cookies.set. Something like event.responseGenerated() or event.headersSent(), since in many cases, unnecessary work can be completely skipped if the event has already been handled / responded to. Let me know if you want me to submit that as a separate feature request.
Reproduction
Reproduction repository:
https://github.com/evestera/sveltekit-headers-sent-repro
Commit of changes from "skeleton" project:
https://github.com/evestera/sveltekit-headers-sent-repro/commit/a37b3b1c6b4997b08575028499da587da6121d5b
src/routes/a/b/+page.server.ts redirects, while src/routes/a/+layout.server.ts inspects the two functions after a short delay.
Logs
No response
System Info
System:
OS: macOS 14.2.1
CPU: (8) arm64 Apple M1 Pro
Memory: 73.73 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 21.6.1 - ~/Library/Caches/fnm_multishells/76568_1707148995605/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 10.2.4 - ~/Library/Caches/fnm_multishells/76568_1707148995605/bin/npm
pnpm: 7.30.3 - /opt/homebrew/bin/pnpm
bun: 1.0.20 - ~/.bun/bin/bun
Browsers:
Brave Browser: 116.1.57.47
Chrome: 121.0.6167.139
Safari: 17.2.1
npmPackages:
@sveltejs/adapter-auto: ^3.0.0 => 3.1.1
@sveltejs/kit: ^2.0.0 => 2.5.0
@sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.2
svelte: ^4.2.7 => 4.2.10
vite: ^5.0.3 => 5.0.12
Severity
annoyance
Additional Information
No response
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sveltejs/kit
-
awaiting submitter
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
documentation pkg:adapter-vercel
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Similar issues
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100