13

Is it possible to have Cache Control but only for static assets like image, font, css and js?

Here's my workaround

[[headers]]
  for = "/*" # This defines which paths this specific [[headers]] block will cover.
  [headers.values]
    Cache-Control = "public, max-age=604800"

it preety much works but not as I expected. The site seems to use the old version even when I updating the content.

1 Answer 1

30

You've now said that the browser should cache every file, including index.html, for a week, for anyone who has visited your site. So, you'll see the old copy of your site for that long.

This is probably not what you want. A better way to do it is to create several header rules, one for each type:

[[headers]]
  for = "*.js" # js files should be set this way
  [headers.values]
    Cache-Control = "public, max-age=604800"
[[headers]]
  for = "*.css" # css files too
  [headers.values]
    Cache-Control = "public, max-age=604800"

However, you may not want to do even this. Netlify sets the caching very intentionally to max-age of 0 but it does allow content to be cached AND enables atomic rollbacks and deploys. Here's the details about that: https://www.netlify.com/blog/2017/02/23/better-living-through-caching/

12
  • 10
    Actually if you fingerprint your assets (js, css, images etc), as it is done by many bundlers (for example webpack), you absolutely should set max-age to a large value or even use "Cache-Control: immutable". Oct 7, 2018 at 13:23
  • 3
    I don't think it, I know it. I work for netlify and wrote the article that is linked in my response. The more specific reason for it is that if you change the filename but not the content, browsers will redownload it unnecessarily. Our cache handling doesn't mind if you change main.css on some deploys - it will force a new download if new content, and will not if it isn't. Unlike "new filename, no cache present, MUST download after each deploy"
    – fool
    Dec 10, 2018 at 21:31
  • 1
    @fool so the slowdown only happens if a file name is changed but the content is not? Wouldn't the file name's fingerprint change only if the content is changed? Dec 23, 2018 at 22:07
  • 3
    @TomaNistor that file is not the problem - it's EVERY OTHER FILE which all have a checksum change due to that filename changing and it being included by name. Also - while you'd think browsers could be smart enough, in fact, they won't request the old filename, so their cache lookup won't match, and they'll redownload it anyway.
    – fool
    Dec 25, 2018 at 22:59
  • 10
    Note that Netlify's approach doesn't work very well at least for certain assets. Specifically, when using webfonts there is a "flash of unstyled text" because the browser revalidation of the font files takes a bit too long. I added headers for .woff2 files and it's a much better experience. The entire scheme also assumes a good connection that's reasonably fast, but many people don't have that (esp. on mobile). Netlify's approach makes sense for some, but I don't think it's a panacea that will work well for everyone/every site. May 19, 2019 at 22:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.