Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netlify Headers Cache Control For Static Assets

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.

like image 827
Yudy Ananda Avatar asked Sep 13 '18 07:09

Yudy Ananda


People also ask

Does Netlify Cache images?

By default Netlify don't cache files which might be good for HTML but not so good for fonts or images.

What is static caching?

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.

How do I add a header in Netlify?

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.


1 Answers

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/

like image 134
fool Avatar answered Sep 22 '22 12:09

fool