Prerendered +server routes don't use the specified HTTP response headers
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the bug
Prerendering a +server.ts file causes its HTTP response headers to be ignored.
E.g., I want to prerender an atom.xml file for a blog site. I'd like to override the Cache-Control and Content-Type headers. When I add export const prerender = true and run pnpm preview, this page renders with content-type: text/html and no cache-control header at all:
import type { RequestHandler } from './$types';
export const prerender = true;
export const GET: RequestHandler = async ({ url }) => {
const body =
'<?xml version="1.0" encoding="utf-8" ?>\n<feed xmlns="http://www.w3.org/2005/Atom">\n</feed>';
const headers = {
'Cache-Control': `max-age=0, must-revalidate`,
'Content-Type': 'application/atom+xml',
};
return new Response(body, {
status: 200,
headers,
});
};
Workaround is to only prerender HTML pages since their content type is text/html, and only those that don't need header overrides such as Cache-Control.
Reproduction
https://github.com/BMorearty/sveltekit-prerender-headers
Clone this repo and build it for production (bug does not reproduce in dev mode):
$ pnpm install && pnpm build && pnpm preview
Then in your browser, open the Network debug panel.
Then go to http://localhost:4173/atom.xml. Note in the Network panel that the Content-Type
is text/html when i should be application/atom+xml, and there is no Cache-Control
header. Both headers are specified in /src/routes/atom.xml/+server.ts but are being ignored.
Also note that atom.xml renders as an HTML page, not as XML. That's due to the incorrect
Content-Type.
Logs
No response
System Info
System:
OS: macOS 13.2.1
CPU: (8) arm64 Apple M1
Memory: 226.09 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.1/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
Browsers:
Chrome: 110.0.5481.177
Firefox: 110.0.1
Safari: 16.3
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.0.0
@sveltejs/kit: ^1.11.0 => 1.11.0
svelte: ^3.56.0 => 3.56.0
vite: ^4.1.4 => 4.1.4
Severity
serious, but I can work around it
Additional Information
No response
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.
Research direction
Start with the reproduction commands and inspect /src/routes/atom.xml/+server.ts, then use the production preview Network panel to trace why the response headers are lost. Done means the prerendered atom.xml response retains application/atom+xml and Cache-Control headers instead of rendering as text/html.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend, web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100