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

Prerendered +server routes don't use the specified HTTP response headers #9408

Open
BMorearty opened this issue Mar 11, 2023 · 9 comments
Open
Labels
feature request New feature or request

Comments

@BMorearty
Copy link

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

@dummdidumm
Copy link
Member

This is a general prerender limitation. Prerendering means the result is on disk as a static asset, which means whatever serves those assets is in charge of defining those headers - there's no SvelteKit-aware runtime involved that could add these.
The content-type at least should be fine on most deployment platforms because they know what content-type to give a .xml file.

@BMorearty

This comment was marked as off-topic.

@jasongitmail

This comment was marked as off-topic.

@eltigerchino

This comment was marked as off-topic.

@BMorearty

This comment was marked as off-topic.

@Rishab49

This comment was marked as off-topic.

@jasongitmail

This comment was marked as off-topic.

@benmccann

This comment was marked as off-topic.

@benmccann benmccann added the feature request New feature or request label Oct 7, 2023
@benmccann
Copy link
Member

benmccann commented Jan 14, 2024

there might be some trickery around CSP nonces to figure out if we did this: #9235

though this is a feature that could never be supported by adapter-static and would be limited to things like adapter-vercel/adapter-netlify where we can set headers for prerendered pages

for adapter-static, we could try to detect if the user sets headers as suggested in #8428 (comment) and warn (for user-enabled prerendering) or opt-out of prerendering (for automatically-enabled prerendering) in that case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants