designwithfocus

Domains, hosting & DNS

How to Use Content Delivery Network Caching on a Site

By Design With Focus Team 6 min read
A browser request hitting an edge cache before the origin server

Short answer

Use a content delivery network by putting its proxy in front of the origin, caching static files at the edge, and purging a URL when that file changes. Cloudflare's docs treat images, CSS, and JavaScript as cacheable by default and leave HTML uncached until you add a rule. Amazon CloudFront is the same idea with a distribution and an origin. No speedup percent belongs in the steps.

Last verified against the sources listed below.

Key takeaways

  • A CDN stores a response closer to the visitor so the origin does not rebuild it every time.
  • Cloudflare caches by file extension. HTML and JSON are not cached by default.
  • Proxied DNS records send web traffic through Cloudflare. DNS-only records do not.
  • Without a cache header, Cloudflare's default edge TTL for a 200 response is 120 minutes.
  • A purge response of HTTP 200 means the request was received, not that a file was evicted.
  • CloudFront's default object lifetime in an edge is 24 hours unless the origin sets otherwise.

How to use content delivery network caching is a DNS and header change, not a new copy of the site. The origin still holds the files. The CDN stores responses for requests it is allowed to share, then answers the next visitor from that copy.

MDN describes a shared cache as a store between the client and the origin that can reuse a response for many users. A private cache, usually the browser, is allowed to hold a response marked private. A CDN is the shared kind. Do not put a logged-in page there.

What you need

You need the apex domain at a registrar where you can change nameservers, and an origin that already answers for the site. Web hosting is that origin. The CDN does not replace it.

You also need the DNS records that matter: the web address, www if you use it, and mail records if the domain sends or receives mail. Cloudflare’s onboard guide says a scan is not guaranteed to find every record, so keep a copy from the old DNS host before you switch.

Shrink large images before they are worth caching. An image compressor does that on the file itself. The CDN will not invent a smaller original.

Put Cloudflare in front of the site

Cloudflare’s onboard guide is a full DNS setup for a domain you already own. One action per step:

  1. Log in to the Cloudflare dashboard and choose Onboard a domain.
  2. Enter the apex, such as example.com, and continue.
  3. Select a plan. The cache docs list Free next to Pro, Business, and Enterprise.
  4. Review the imported DNS records and add any the scan missed, including mail.
  5. Set the website A, AAAA, or CNAME records to Proxied so traffic hits the edge.
  6. Leave MX and other mail records on DNS only.
  7. If DNSSEC is on at the registrar, turn it off before the nameserver change.
  8. Replace the registrar nameservers with the two Cloudflare assigns.
  9. Request a CSS or image URL and look for a cache status on the response.
  10. When you change a file, purge that URL instead of waiting out the TTL.

The guide says many services start protecting and speeding up traffic after the nameservers update and the records are proxied. It does not publish a percent. Page speed is the separate question of how you measure the result.

What gets cached

Cloudflare caches by extension. The default list includes CSS, JS, JPG, PNG, GIF, WEBP, SVG, WOFF, and WOFF2, among others. HTML and JSON are not on that default list. robots.txt is cached by default even though it is not an image.

The origin can refuse the cache. Cloudflare does not cache a response when Cache-Control is private, no-store, no-cache, or max-age=0, when a Set-Cookie header is present, or when the method is not GET. It does cache when Cache-Control is public and max-age is greater than 0, or when Expires is in the future. If both max-age and Expires are set, max-age wins.

If those headers are absent, the default edge TTL is 120 minutes for status 200, 206, and 301. Other codes differ, and many codes are not cached at all. Free, Pro, and Business plans cache files up to 512 MB. The Free plan’s max upload size on that page is 100 MB.

When the origin is already on AWS

CloudFront is the same pattern with different nouns. You name an origin, which can be an S3 bucket or an HTTP server, upload the objects, and create a distribution. CloudFront assigns a domain name such as d111111abcdef8.cloudfront.net, or you attach your own name. The configuration, not the files, is sent to the edge locations.

The introduction says a file stays at an edge for 24 hours by default. The minimum expiration is 0 seconds, and there is no maximum. CloudFront charges for data transfer out to viewers and for HTTP or HTTPS requests.

Transfer from an AWS origin such as S3, Elastic Load Balancing, or API Gateway to CloudFront is free. The introduction does not print a per-gigabyte rate, so this guide does not invent one. The CloudFront pricing page is where that rate lives.

Use a standard distribution for one site. The multi-tenant option is for a SaaS provider managing many similar sites. Most people, the page says, start with a standard distribution.

When the cache misbehaves

Read the header before you change the plan. CF-Cache-Status of HIT means Cloudflare served a stored copy.

What you see What the docs point to What to change
HTML is never served from the edge HTML is not cached by default Add a Cache Rule only for HTML that is safe to share
A CSS or image file misses every time Cache-Control blocks the cache, or the response sets a cookie Relax the header on files that are not personal
The CDN never sees the request The DNS record is DNS only Proxy the web record. Leave mail alone
Mail fails after the switch An MX or mail record was proxied Set mail records back to DNS only
Visitors still see the old file The previous object is in the edge Purge that URL, then confirm the status is not HIT
A purge call returns 200 but nothing changed 200 means the request was accepted Request the URL again and check CF-Cache-Status

Personalized pages belong in a private cache. MDN says a private directive keeps the response off shared caches. That is the right header for a logged-in account page, not a bug to route around.

Frequently asked questions

Does a CDN cache the HTML of every page?

Not on Cloudflare's defaults. The default cache behavior page says the CDN caches by file extension, not by MIME type, and that it does not cache HTML or JSON by default. CSS, JavaScript, and common image extensions are on the cached list. HTML can be cached if you add a Cache Rule, which the getting started page points to for static HTML or anonymous page views. Leave personalized HTML uncached. A Set-Cookie header also keeps a response out of the cache.

What does the orange-cloud proxy actually change?

Cloudflare's onboard guide says a proxied record sends web traffic through Cloudflare, which is what provides caching and the other edge features. A DNS-only record returns the origin address and does not proxy the request, so the CDN never sees it and cannot cache it. Mail records should stay DNS-only. The guide's sample MX, SPF, and DKIM rows are marked DNS only for that reason. Proxying mail is a common way to break delivery after the cutover.

How long does a cached file stay at the edge?

Cloudflare says it respects Cache-Control and Expires from the origin unless an Edge Cache TTL rule overrides them. If neither header is present, a 200, 206, or 301 is cached for 120 minutes, a 302 or 303 for 20 minutes, and a 404 or 410 for 3 minutes. CloudFront's introduction says each file stays in an edge location for 24 hours by default before it expires. You can set that lifetime on the origin. The minimum CloudFront expiration is 0 seconds, and the page says there is no maximum.

How do I know a purge worked?

Cloudflare's purge page says a successful purge returns HTTP 200, and that this only means the request was received. It does not prove the file had been cached or that it was removed. The check the page gives is to request the asset again and confirm the CF-Cache-Status header is no longer HIT. Single-file purge by URL is the method the page recommends. On the Free plan, hostname, tag, prefix, and purge-everything requests are limited to 5 per minute.

Sources