sveltejs / sveltejs/kit

Incorrect "Cache-Control" Error Message in SvelteKit when Protected Route has Import Issue

Open
#11,676 0 comments 0 reactions 0 assignees View on GitHub

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

I believe there might be a bug in SvelteKit, and I've created a test case to illustrate the issue.

Here's the scenario: I aim to establish global cache headers, but with exceptions for protected routes. The plan is to define the global cache headers in the +layout.server.ts file. However, if the route is protected, I want to override the headers to be set as "no-store."

To implement this, I include the headers in +layout.server.ts and then create a handle function in hooks.server.ts to modify the headers specifically for protected routes.

The problem arises when a module is incorrectly imported in the protected route. In such cases, the script fails, but the error message is misleading. Instead of indicating the import issue, it displays an unrelated error message: "Error: "Cache-Control" header is already set."

Please load the project and go to http://localhost:5173/protected. To see the change in behavior go to /src/routes/protected/+page.svelte and modify the file

Reproduction

https://github.com/Grupo-Viteri/svelte-header-cache-bug

Logs
Error: "Cache-Control" header is already set
    at setHeaders (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:199:12)
    at load (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:6:3)
    at Module.load_server_data (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:48:42)
    at Module.respond_with_error (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:57:54)
    at async Module.render_page (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:317:10)
    at async resolve (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:432:18)
    at async dontCachePage (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:9:20)
    at async Module.respond (eval at instantiateModule (file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/vite@5.0.11/node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:50858:28), <anonymous>:305:20)
    at async file:///C:/Users/Ricardo/Desktop/softwareProjects/web-asimed-sveltekit/Cache-header%20bug/node_modules/.pnpm/@sveltejs+kit@2.3.5_@sveltejs+vite-plugin-svelte@3.0.1_svelte@4.2.9_vite@5.0.11/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:502:22
System Info
System:
    OS: Windows 11 10.0.22631
    CPU: (8) x64 Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
    Memory: 23.10 GB / 39.84 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.10.2 - ~\AppData\Local\pnpm\pnpm.CMD
  Browsers:
    Edge: Chromium (120.0.2210.144)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.1.0
    @sveltejs/kit: ^2.0.0 => 2.3.5
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1
    svelte: ^4.2.7 => 4.2.9
    vite: ^5.0.3 => 5.0.11
Severity

serious, but I can work around it

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Load the reproduction project and visit http://localhost:5173/protected. Start with src/routes/+layout.server.ts, hooks.server.ts, and src/routes/protected/+page.svelte, then modify the protected page to reproduce the incorrect import. Done means the import failure is reported instead of the unrelated "Cache-Control" header error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.