-
Notifications
You must be signed in to change notification settings - Fork 496
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
feat: set default Cache-Control header with max-age of 1 year #763
Conversation
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
// const TWO_DAYS = 2 * 60 * 60 * 24 | ||
// event.res.setHeader('Cache-Control', `max-age=${TWO_DAYS}, immutable`) | ||
// } | ||
if (isPublicAssetURL(id) && !event.res.getHeader("Cache-Control")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for a public asset base of /
(which would be the case with Nuxt's /public
directory) this will cache all html/assets, including things like robots.txt
, favico.ico
or even /about/index.html
. I think that is probably too broad a net to cast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @pi0 has some thoughts about this, but maybe allow cache headers to be set per asset folder? So Nuxt could push in /_nuxt
, for example, with the appropriate long cache headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I have this accomplished in Nuxt 3 using routeRules
currently:
routeRules: {
'/assets/**': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/images/**': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/_nuxt/**': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.js': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.css': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.json': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.html': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.xml': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
'/**/*.svg': { headers: { 'Cache-Control': 'max-age=31536000, immutable' } },
}
Perhaps nitro's scope is too broad to set any cache defaults since it could be used by things other than Nuxt 3? Maybe these defaults (or something perhaps more targeted) could be done on the Nuxt config side, rather than here in Nitro?
Thanks for PR dear @nathanchase As of above discussion, i think using route rules indeed makes much better sense and makes it possible to also support via CDN native configurations without hitting the server. I think we can still support it in nitro via |
π Linked issue
#762, nuxt/nuxt#15465
β Type of change
π Description
For all public static assets, we can define a best practice value for the Cache-Control header, to fall in line with the Google Lighthouse scoring of serving assets with an efficient cache policy.
Cache-Control: max-age=31536000, immutable
This checks if the asset is public, and not already defined, and sets the header accordingly.
π Checklist