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

feat: set default Cache-Control header with max-age of 1 year #763

Closed

Conversation

nathanchase
Copy link
Contributor

@nathanchase nathanchase commented Dec 17, 2022

πŸ”— Linked issue

#762, nuxt/nuxt#15465

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@vercel
Copy link

vercel bot commented Dec 17, 2022

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Updated
unjs-nitro πŸ”„ Building (Inspect) Dec 17, 2022 at 7:10PM (UTC)

// 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")) {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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?

@pi0
Copy link
Member

pi0 commented Jan 20, 2023

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 publicDir.maxAge option mapping to (auto) route rules. Making a PR shortly. (#860)

@pi0 pi0 closed this Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants