How do we control the cache headers of an asset?


(Serge) #1

Very simple, where is the control for caching header on assets? (img, js, css, etc). I can’t seem to find them.

This has been asked before:


Failing that, I’m assuming Matrix has a best practice on how to force cache flushing, like putting some random ID in the URL so the updated is loaded from the browser:

http://domain.com/12345/style.css
… update …
http://domain.com/12346/style.css
… update …
http://domain.com/12347/style.css

… that would force the reload.

Anyway, how is this all handled?


(Douglas (@finnatic at @waikato)) #2

Caching headers on static assets AFAIK is dependent on the underlying HTTP server (Apache, Nginx).

In terms of cache flushing, we’ve discussed with Squiz using a cachebuster suffix on CSS and JS files but there’s no standard guide on how to set that up AFAIK, and it seems to be a topic for debate - if you have Squizmap access have a look at https://squizmap.squiz.net/matrix/9440


(Serge) #3

Nope, PHP can override
https://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php

ooohh… I see in the squizmap, looks like there have an explicit directive there in the server config. mmmmm… that’s a weird design decision, cache control is a really important feature admins need to have control over.


(Serge) #4

Yeah I was thinking we could have a $version variable there that would increment on update then asset linked via asset_id would show in browser as
http://domain/path/file.ext.<$version>
ex: http://domain/path/file.ext.12345


(Marcus Fong) #5

I think you and Douglas are talking about different things there, Serge - in the same way that the two questions you cited are actually about specific different aspects of the system.

As you’ve said, cache headers can be sent by PHP, and that’s what Matrix does (with an exception that I’ll discuss below). This is controlled from the Cache Manager asset, which has a few different levels:

https://matrix.squiz.net/manuals/system-management/chapters/cache-manager#cache-options
https://matrix.squiz.net/manuals/system-management/chapters/cache-manager#Type-Code-Specific-Screen
https://matrix.squiz.net/manuals/system-management/chapters/cache-manager#Root-Node-Specific-Screen

As far as I’m aware, the order of precedence there is that Type Code specific settings override Root Node specific settings which override the baseline Cache Options.

What Douglas is referring to is specifically regarding FIle assets (and descendant types) where Unrestricted Access is set to Yes, status is Live and Public User read permissions have been granted. This triggers a specific performance optimisation in Matrix, where the file is hardlinked into the data/public directory.

The data/public directory is served as the /__data/ URL directly from the webserver, cutting out all PHP code execution. While this is much more efficient performance-wise than having to execute Matrix PHP code just to serve a file that had no access restrictions anyway, it does mean that you do need to set cache headers for that directory in the webserver configuration. This is the directive you’re seeing in the server config.


(Serge) #6

Yeah but could we have a trade-off of that type:

/__data/expire10m/ {
… set cache header to expire after 10 mins …
root /__data/static/
}

/__data/expire30m/ {
… set cache header to expire after 30 mins …
root /__data/static/
}

/__data/expire60m/ {
… set cache header to expire after 60 mins …
root /__data/static/
}

… and so on. Then in matrix have a setting on the static file that moves it under the right folder so it is served with the right caching headers. Then matrix just has to refer by the right URL.

Ex:

  1. we got a js file, say abc.js
  2. that file has default path http://domain/__data/expire0m/abc.js
  3. In matrix, right-click on asset > settings > set cache expiry [0m, 10m, 30m, 60m, … and so on] > pick 30m
  4. Matrix rewrites url to http://domain/__data/expire30m/abc.js