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.
By default Netlify don't cache files which might be good for HTML but not so good for fonts or images.
While many forms of caching are available, static caching is a method for converting the page generated by a user's request into an HTML document to serve any subsequent requests to that same page.
You can configure custom headers for your Netlify site in two ways: Save a plain text file called _headers to the publish directory of your site. You can find _headers file syntax details below. Add one or more headers tables to your Netlify configuration file.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With