Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: mutating the response in the server handle function #11883

Open
hyunbinseo opened this issue Feb 22, 2024 · 0 comments · May be fixed by #12543
Open

docs: mutating the response in the server handle function #11883

hyunbinseo opened this issue Feb 22, 2024 · 0 comments · May be fixed by #12543
Labels
documentation Improvements or additions to documentation

Comments

@hyunbinseo
Copy link
Contributor

hyunbinseo commented Feb 22, 2024

Describe the bug

The example from the server hooks documentation can cause the following error:

TypeError: immutable
at _Headers.set (node:internal/deps/undici/undici:2180:17)
at Object.handle (/src/hooks.server.ts:3:19)

docs: issue since setting headers to ALL responses in the handle function seems dangerous.

Reproduction

// src/routes/+server.ts
export const GET = ({ url }) => Response.redirect(`${url.origin}/${Date.now()}`);
// src/hooks.server.ts
export const handle = async ({ event, resolve }) => {
  const response = await resolve(event);
  response.headers.set('x-custom-header', 'potato');

  return response;
};
pnpm dev --open

Simpler reproduction with identical error:

Response.redirect('https://kit.svelte.dev/').headers.set('x-custom-header', 'potato');

Logs

No response

System Info

System:
  OS: macOS 14.2.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 40.55 MB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.11.1 - ~/.volta/tools/image/node/20.11.1/bin/node
  npm: 10.2.4 - ~/.volta/tools/image/node/20.11.1/bin/npm
  pnpm: 8.15.3 - ~/.volta/bin/pnpm
Browsers:
  Chrome: 121.0.6167.184
  Edge: 121.0.2277.128
  Safari: 17.2.1
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.1.1 
  @sveltejs/kit: ^2.0.0 => 2.5.1 
  @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.2 
  svelte: ^4.2.7 => 4.2.11 
  vite: ^5.0.3 => 5.1.4

Severity

annoyance

Additional Information

Removing a response header as a solution:

Response.redirect() spec does not allow modifying the header:

Adding a try-catch block can be a viable documentation fix.

try {
  response.headers.delete('link');
} catch {
  // TypeError: immutable
}
@eltigerchino eltigerchino added the documentation Improvements or additions to documentation label Feb 26, 2024
@hyunbinseo hyunbinseo linked a pull request Aug 4, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants