public directory no cache-control #86419
Replies: 5 comments
-
|
Yeah, the statement is correct. Next.js does not automatically apply any cache headers to files in So yes, if you don’t set On Vercel, caching works because Vercel adds the headers for you. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Really? Just to confirm — files inside the /public directory in Next.js are not automatically cached by the CDN or the framework. They are only cached if I manually configure cache headers at the server level (e.g., Vercel headers, Nginx, Cloudflare). Is that correct? |
Beta Was this translation helpful? Give feedback.
-
|
You are correct — Next.js does not automatically set cache headers for files in the How to handle caching:
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
]
}
]
}This will tell the CDN and browser to cache the files for 1 year.
app.use(express.static('public', {
setHeaders: (res, path) => {
res.set('Cache-Control', 'public, max-age=31536000, immutable');
}
}));✅ Summary
This approach ensures that both browsers and CDNs handle caching correctly. |
Beta Was this translation helpful? Give feedback.
-
|
✔ If you deploy to Vercel Vercel automatically applies CDN caching to /public assets with long-term immutable caching: Cache-Control: public, max-age=31536000, immutable So on Vercel, they ARE cached automatically, even though Next.js does not set these headers directly. ✔ If you self-host (Node.js server, Docker, Nginx, etc.) Then you must manually set cache headers, because Next.js does NOT do it for you. So the real answer is: Next.js alone → no automatic cache headers Vercel → adds CDN caching automatically Self-hosting → must configure caching yourself |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Next.js does not automatically set cache headers for files in the public directory. You must configure the cache headers yourself at the server or CDN level.
You can use this sentence to ask for official confirmation.
Really? Because files in the public directory are not cached by the CDN unless I manually set the cache headers on the server or CDN. Is that correct?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions