Home About News Products Blog

Fixing "Leverage browser caching" in PageSpeed Insights


#1

If you see a low PageSpeed Insights score and see warnings about “Leverage browser caching” in the list of items to improve:
image

Pagespeed Inisghts requires you to set browser time for over 7 days.
See: https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
"We recommend a minimum cache time of one week and preferably up to one year for static assets,"

You can use Varnish to fix the cache-control header before sending to the browser:

You can use the above code in conjunction with static caching code to improve both PageSpeed Insights score, page load time as well as reduce data usage to the origin.

Keep in mind that the above fixes only work for assets that are served from the domain that Varnish is serving, not 3rd party domains. So if your site is www.example.com, then that above fix will work for all assets from www.example.com, it won’t have affect on assets from www.google-analytics.com as an example.