Website caching and CDNs: what they are and do you need one?

Last updated

The short answer

Caching means keeping a ready copy of your pages and files so they don't have to be built or downloaded again. There are three kinds: the visitor's browser cache, a page cache on your server, and a CDN that keeps copies close to your visitors. Most small-business sites should tell browsers to keep images and fonts for a week and scripts and stylesheets for a day, switch on their platform's page caching, and consider a CDN if visitors come from far away or the host is slow.

The three kinds of caching

KindWhere the copy livesWhat it speeds up
Browser cacheThe visitor's own browserTheir second page and their next visit: images, fonts and scripts aren't downloaded again
Page cacheYour server or hostEvery visit: a finished page is served instead of being built from the database each time
CDNServers around the worldVisitors far from your host, and busy periods, because files come from a nearby copy

Browser caching is controlled by the Cache-Control header your server sends with each file (web.dev explains it well).

The Cache-Control settings to use

WhatSettingWhy
Pages (HTML)no-cacheStored, but re-checked on every visit, so visitors always get the latest page
Images and fontspublic, max-age=604800Kept for a week
Scripts and stylesheetspublic, max-age=86400Kept for a day, so updates appear the next day at the latest
Files whose name changes when updatedmax-age=31536000, immutableKept for a year: a new version gets a new name, so the old copy is never wrong
Account, basket and checkout pagesno-storeNot stored at all. private only keeps them out of shared caches such as a CDN: the visitor's browser may still store them. Also set your page cache and CDN to skip these pages.

no-cache doesn't mean "don't cache": as MDN puts it, the copy can be stored but "must be validated with the origin server before each reuse". no-store is the one that means nothing is kept.

How to set it, by platform

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 week"
  ExpiresByType image/jpeg "access plus 1 week"
  ExpiresByType image/png "access plus 1 week"
  ExpiresByType image/svg+xml "access plus 1 week"
  ExpiresByType font/woff2 "access plus 1 week"
  ExpiresByType text/css "access plus 1 day"
  ExpiresByType application/javascript "access plus 1 day"
</IfModule>

Do you need a CDN?

A CDN is worth it when:

If your visitors are local and your host is close and quick, good browser and page caching may be enough. Many hosts include a CDN, and services such as Cloudflare put one in front of any site. A CDN also helps the Largest Contentful Paint speed score by getting the main image to the browser sooner.

Mistakes to avoid

Checklist

Questions people ask

What is website caching?
Keeping a ready copy of a page or file so it doesn't have to be built or downloaded again: in the visitor's browser, on your server, or on a CDN near the visitor. It makes repeat visits and busy periods much faster.
Will caching show visitors an old version of my site?
It can. Pages themselves are re-checked on each visit, but a browser may keep an image, stylesheet or script until its time runs out. When you change a file, publish it under a new name (many site builders and plugins add a version number for you) and make the page point to it. Purging a CDN doesn't clear copies already held in visitors' browsers.
Is Cloudflare a CDN?
Yes. It is one of several CDN services, and many web hosts include a CDN with their plans. Check what your host offers before adding another.
Should I cache pages for logged-in customers?
No. Pages showing account details, baskets or checkout should be marked no-store and kept out of the page cache and CDN, so one customer never sees another's page.

What the free scan checks for this

These are the FocalScore checks that cover this guide's subject. Each one in your report says what it found, shows the evidence and gives the fix.

Sources

Last checked . Documentation, settings and rules change; if one of these pages now says something different, it is right and this guide is out of date. Tell us at info@focalscore.com.

  1. web.dev: The HTTP cache
  2. MDN: Cache-Control
  3. web.dev: Largest Contentful Paint