Improve cache policy

Every site I serve from GitLab pages gets a Serve static assets with an efficient cache policy warning from Lighthouse. I’ve seen this issue which says that there isn’t currently a way of setting the HTTP headers from within GitLab.

Is there some other way around this, other than deploying elsewhere? Can something like CloudFlare be used here?



I don’t use Gitlab pages, but I do use Cloudflare. Caching in Cloudflare I have set to 6 months. What I do find is that if I use curl -I on my site:

cache-control: max-age=172800

which is 2 days. So I’m not sure why the headers say 2 days, when configured for 6 months. In addition to this I do have Apache configured with expires:

ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"

obviously the last part you cannot do, but if you do have a Cloudflare account, and you have your DNS managed by Cloudflare, you could create the relevant entries in your DNS that Gitlab requires for pages, and ensure those entries are proxied via cloudflare. Then set the caching to 6 months, and check in Lighthouse again. If the results are better, then maybe for you the utilisation of Cloudflare would be enough. If not, then it may need server-side changes similar to what I did with Apache. I have those settings in Apache prior to using cloudflare, so they are still there. Whether Lighthouse actually sees them or not, or they get overridden by Cloudflare I’ve never really checked it.

With Lighthouse though I do have high scores, and no errors in regard to caching.

Hope that helps a little.