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

Missing Cache-Control header in /404 rewrite response #76168

Open
thomasdax98 opened this issue Feb 18, 2025 · 2 comments
Open

Missing Cache-Control header in /404 rewrite response #76168

thomasdax98 opened this issue Feb 18, 2025 · 2 comments
Labels
Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@thomasdax98
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/nextjs-404-missing-cache-control-xvggzv

To Reproduce

  1. Go to terminal
  2. Stop Dev server
  3. Start Next in production mode: npm run build && npm start
  4. Execute curl -I http://localhost:3000/example

Current vs. Expected behavior

Current behavior

The response doesn't include a Cache-Control header:

Image

Expected behavior

The reponse should include an explicit Cache-Control header. I propose private, no-cache, no-store, max-age=0, must-revalidate to avoid caching.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4242
  Available CPU cores: 2
Binaries:
  Node: 20.12.0
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 15.1.7 // Latest available version is detected (15.1.7).
  eslint-config-next: 15.0.4
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next build (local), next start (local), Other (Deployed)

Additional context

I tested this behavior in Next v14 and v15.

My original motivation for rewriting a path to /404 was rewriting non-existing /_next/static URLs because for those we skip the middleware and they caused 500 errors in my page.tsx.

@samcx
Copy link
Member

samcx commented Feb 19, 2025

@thomasdax98 Not exactly sure yet why rewrites cause the /404 path to miss a Cache-Control, but also I don't think you should need to rewrite non-existing /_next/static URLs (what 500 errors are you seeing?).

@thomasdax98
Copy link
Author

@samcx

but also I don't think you should need to rewrite non-existing /_next/static URLs (what 500 errors are you seeing?).

That's somewhat specific to our system: We host multiple different sites on different domains within one Next.js app. We normally rewrite the URL in the middleware and prefix the URL with "scope" information based on the domain. This allows us to access the domain via the params. Then some configuration is loaded based on this scope.

Normally a request looks something like this: https://domain-one.com/en/my-page. The URL is rewritten to /domain-one/en/my-page -> the scope is { domain: "domain-one" }. In page.tsx we load the config based on this scope:

const config = getSiteConfigForScope({ domain: "domain-one" });

getSiteConfigForScope throws an error if there is no config for the scope -> this is the 500

For /_next/static, we skip the middleware to avoid the URL rewrite. If a /_next/static URL doesn't exist and it's routed to our page.tsx, this happens:

const config = getSiteConfigForScope({ domain: "_next" });

There is no config for this "scope" -> 500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

2 participants