A long cache lifetime can speed up repeat visits to your page

J test my Website speed with pagespeed insights and j got this DIAGNOSTICS

-Serve static assets with an efficient cache policy 31 resources found

“A long cache lifetime can speed up repeat visits to your page”

. How long to set my cache for my website ? b.t.w j am running adsense on my website so any tips about how long to set cache or optimize my website for adsense are welcome. and appreciated

Unfortunately there is no quick answer to that question, and it depends on what you are doing.

In the case of static assets (js, css, images) that never change, setting the max-age to a year of more is perfectly fine.

Cache-Control: max-age=31536000, immutable

In an ideal scenario the URL for an asset would change whenever the content of the asset needed to change, but that is not always the case. Many content management systems will do this automatically for you, with file naming conventions that include an MD5 hash in the filename for such assets (some systems append a version number as a query parameter which does the same thing, but I’m not such a fan of that method).

If your content and assets are changing frequently you obviously cannot set a long cache time.

2 Likes

OK thank you very much for the answer mu assets are static they never change so j will be good to set cache 6 months or 1 years … but what about ads on my website that are changing, the long cache period wont affect them ?

If you are using the standard AdSense implementation then the ads are not affected by the cache time on your website. The AdSense tag is just a static script with parameters unique to you as a publisher. That script contacts Google on every load, and Google return the ads directly to the users browser. The request/response to Google is not subject to the cache rules on your own assets, and does not go through Cloudflare.

1 Like

You can also set custom headers from your origin server too see https://blog.cloudflare.com/cdn-cache-control/

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.