TapCub is live — analytics, insights and live chat, free on one platform
Analytics

Expires Header and Cache-Control: What to Set and How to Check

The Expires header gives the date after which a cached response is stale, but Cache-Control max-age wins when both are present. See the order caches follow, what to set for each file type, how to set it and a curl check.

Lin Zhou Published Nov 20, 2026 12 min read Analytics
Sample data: a 520 KB page (260 KB shared JavaScript, CSS and font; 260 KB unique HTML and images) viewed 5 times. With shared files cached, the first visit transfers 520 + 4 × 260 = 1,560 KB instead of 2,600 KB, and a return visit 5 × 260 = 1,300 KB.Sample data

The Expires header gives the GMT date after which a cached response is stale. Cache-Control: max-age says the same in seconds and wins when both are present, so set max-age and keep Expires as a fallback. Below: the precedence order, values by file type, nginx and Apache setup, and a curl check.

What you will learn

  • Caches compute freshness from s-maxage, then max-age, then Expires minus Date, and only then guess; an invalid Expires such as 0 means already stale
  • Versioned files get Cache-Control: public, max-age=31536000, immutable; HTML and fixed-URL files get no-cache plus an ETag
  • Verify with curl -sI, then send the ETag back with If-None-Match and expect a 304

What the Expires header does

The Expires response header gives the date and time after which a cached copy of the response counts as stale. Until that moment a browser or shared cache may reuse the stored response without asking your server. The value is an HTTP date in GMT, for example Expires: Thu, 31 Dec 2026 23:59:59 GMT.

Two things make it less useful than it looks. First, it is an absolute clock time, so it depends on the server clock and on the Date header the same response carries; the cache works out the freshness lifetime as Expires minus Date. Second, Cache-Control: max-age expresses the same idea as a number of seconds and wins when both are present. For new work, set Cache-Control and treat Expires as the older fallback that some tools and scoring checks still look for.

What a cache does with a stale response is not "throw it away". It sends a conditional request using the validators ETag or Last-Modified, and if the file has not changed the server answers 304 Not Modified with no body. That is why the best configuration pairs a long lifetime for files that never change with a short or zero lifetime plus validators for files that do.

Expires or Cache-Control: which one wins

RFC 9111, section 4.2.1, gives the order a cache must follow when working out how long a response stays fresh: s-maxage (shared caches only) first, then max-age, then Expires minus Date. Only when none of those exists may a cache guess; the RFC says a typical guess is 10% of the time since Last-Modified. And section 5.3 says an invalid Expires date, in particular the value 0, must be treated as a time in the past, that is, already stale.

The five cases below are computed with the small script further down. All use Date: Fri, 20 Nov 2026 12:00:00 GMT.

CaseHeaders presentFreshness lifetimeWhy
ACache-Control: public, max-age=31536000, immutable31,536,000 s (365 days)max-age is used
BExpires: Thu, 31 Dec 2026 23:59:59 GMT only3,585,599 s (41.5 days)Expires minus Date
Cmax-age=3600 and Expires one year ahead3,600 s (1 hour)max-age beats Expires
DLast-Modified: Thu, 20 Aug 2026 12:00:00 GMT only794,880 s (9.2 days)Heuristic, 10% of the 92 days since modification
EExpires: 00 sInvalid date means already stale

Case D is the one to avoid: a response with no explicit freshness information may still be cached by browsers, for a time you did not choose. Case C is the common accident when a server adds a far-future Expires by default and a framework adds a short max-age: the short one wins, silently.

What to set for each kind of file

The rule that covers most sites: files whose name changes when their content changes can be cached for a year; files whose name stays the same must be revalidated.

File typeExample headerReasoning
HTML pagesCache-Control: no-cache plus ETag (or max-age=0, must-revalidate)Links to the other files live here, so it must always be current; revalidation costs one small 304
JS and CSS with a hash or version in the URL (app.4f3a9c.js, site.css?v=a61c33dafb)Cache-Control: public, max-age=31536000, immutableA new build produces a new URL, so the old URL never changes
JS and CSS with a fixed URL (/js/app.js)Cache-Control: no-cache plus ETag, or a short max-age such as 3600Changes would not reach visitors for the whole lifetime otherwise
Images that never changemax-age=31536000, immutable if versioned; otherwise a day to a monthLarge and the biggest saving
Web fontsmax-age=31536000, immutableRarely change, so one year is safe
API responses with personal dataCache-Control: no-storeno-store forbids storing at all, including by CDNs

Google's web.dev guide matches this: for versioned URLs, add Cache-Control: max-age=31536000, the longest value the specification supports; for unversioned URLs use no-cache, which lets the browser keep a copy but requires revalidation every time, and reserve no-store for responses that must never be stored. It also recommends ETag as the more accurate validator. Chrome's Lighthouse audit "Serve static assets with an efficient cache policy" flags fonts, images, media, scripts and stylesheets with a 200, 203 or 206 status and no explicit no-cache policy, and tells you to put a hash in file names so each version is unique.

File typeCache-Control to sendLifetime
HTML pagesno-cache plus ETagRevalidate each use
JS and CSS with a hash or version in the URLpublic, max-age=31536000, immutable1 year
JS and CSS at a fixed URLno-cache plus ETag, or max-age=36000 to 1 hour
Versioned images and fontspublic, max-age=31536000, immutable1 year
API responses with personal datano-storeNever stored

A starting point for most sites. Version the file name first, then lengthen the lifetime.

How much a correct cache saves

Sample data: a page weighs 520 KB: 20 KB HTML, 180 KB JavaScript, 60 KB CSS, 20 KB font and 240 KB images. A visitor views 5 pages in a session. Each page has its own HTML and images; JavaScript, CSS and the font are shared.

  • No cache headers at all, and no heuristic caching: 5 × 520 KB = 2,600 KB.
  • Versioned shared files cached for a year: the first page costs 520 KB; each of the next four costs only HTML and images, 20 + 240 = 260 KB. Total 520 + 4 × 260 = 1,560 KB.
  • Saving: 1 − 1,560 ÷ 2,600 = 40%, and the repeat-visit saving is larger because the next session also reuses the 260 KB of shared files.

The size of the effect depends on how much of a page is shared. A site with a heavy JavaScript bundle shared across pages gains most; a page full of unique photographs gains least. Measure your own split rather than assuming.

app.tapcub.com

Data transferred over 5 page views

sample data, KB
No cache headers, first visit2,600 KB
Versioned files cached, first visit1,560 KB
No cache headers, return visit2,600 KB
Versioned files cached, return visit1,300 KB
Sample data
Sample data: a 520 KB page (260 KB shared JavaScript, CSS and font; 260 KB unique HTML and images) viewed 5 times. With shared files cached, the first visit transfers 520 + 4 × 260 = 1,560 KB instead of 2,600 KB, and a return visit 5 × 260 = 1,300 KB.

How to set the header

nginx: the expires directive sets both headers. expires 24h; produces Expires now plus 24 hours and Cache-Control: max-age=86400; a negative value gives Cache-Control: no-cache; expires max; sets a date in 2037 and a ten-year max-age. Because you usually want immutable too, set the header yourself. expires 1y; alone gives max-age without immutable, and combining it with add_header Cache-Control sends a second Cache-Control line, so use add_header only:

location ~* \.(?:js|css|woff2)$ {
    add_header Cache-Control "public, max-age=31536000, immutable";
}

Apache: enable mod_expires, then use ExpiresActive On and ExpiresByType, for example ExpiresByType image/png "access plus 1 years". The module generates both Expires and Cache-Control: max-age. If a response already has an Expires or Cache-Control header, the module leaves it alone. To add immutable, use mod_headers with the Header directive.

CDNs and hosting platforms: most let you set the browser lifetime separately from the CDN's own lifetime. Use s-maxage for the CDN and max-age for the browser if you need them to differ. And keep the header on the origin too, so you do not depend on one provider's defaults.

How to check the headers with curl

Request only the headers, then look at three lines:

curl -sI https://example.com/assets/app.4f3a9c.js | grep -iE "^(cache-control|expires|etag|last-modified|date|age)"

A real response from tapcub.com on 20 November 2026 shows the pattern for a versioned stylesheet (/assets/css/site.css?v=...):

cache-control: public, max-age=31536000, immutable
etag: "74901612811fa444ef18ec9e3626f698"

and for the HTML home page:

cache-control: public, max-age=0, must-revalidate

Then test revalidation by sending the validator back:

curl -sI -H 'If-None-Match: "74901612811fa444ef18ec9e3626f698"' https://example.com/assets/app.4f3a9c.js

An unchanged file answers 304 Not Modified. A 200 means the server ignores If-None-Match, so every stale visit re-downloads the whole file. In the browser, the Network panel's Size column reads "(disk cache)" or "(memory cache)" for a fresh hit and shows a small transfer for a 304.

To compute the freshness lifetime from a header set, as in the table above:

import re
from email.utils import parsedate_to_datetime as pd

def lifetime(h):
    h = {k.lower(): v for k, v in h.items()}
    m = re.search(r'max-age=(\d+)', h.get('cache-control', ''))
    if m: return int(m.group(1))
    if 'expires' in h:
        try: return max(0, int((pd(h['expires']) - pd(h['date'])).total_seconds()))
        except Exception: return 0            # invalid date: already stale
    if 'last-modified' in h:
        return int(0.1 * (pd(h['date']) - pd(h['last-modified'])).total_seconds())
    return 0

Common mistakes

  • A far-future lifetime on a file whose URL never changes. Visitors keep the old CSS for a year after you fix it. Fix: add a hash or version to the URL, or switch the file to no-cache.
  • A long max-age on HTML. The page keeps pointing to old file names. Use no-cache or max-age=0, must-revalidate for HTML.
  • Confusing no-cache and no-store. no-cache means "store it, but check before using it"; no-store means "do not store it". Using no-store on static files throws away the whole benefit.
  • Two conflicting headers. max-age=3600 next to an Expires a year ahead behaves as one hour (case C above). Check the final response from the server your visitors reach, not the config file.
  • No validators. A stale response with neither ETag nor Last-Modified cannot be revalidated, so every expiry costs a full download.
  • Caching responses with cookies or personal data on a shared cache. Use private or no-store.

How to see whether caching helped in TapCub

TapCub does not set your headers and is not a CDN; it measures what real visitors experience after you change them.

  1. Take a baseline. Open Monitoring › Performance, which shows real-user Core Web Vitals. Note the P75 largest contentful paint and the share rated good (2.5 seconds or less) for your slowest pages, using the Slowest pages list.
  2. Mark the change. Add a date annotation under Configuration › Annotations on the day you deploy the cache headers so the chart shows when it happened.
  3. Read load timing. The report splits a load into backend (time to first byte), network transfer and frontend render. Caching static files mainly shrinks network transfer for repeat visitors; if the time to first byte is the slow part, headers will not fix it.
  4. Compare after a week. Return to the same pages and the same P75. Percentiles jump when a page has few samples, so judge pages with enough traffic. If the number did not move, check with curl that the headers reached visitors.

Background on speed and rankings is in how to optimize website performance, and on image weight in optimize images for the web.

FAQ

What is the Expires header?

It is an HTTP response header with a date and time, in GMT, after which the cached response is stale. Until then a browser or shared cache may reuse it without contacting the server. If the response also has Cache-Control: max-age, that takes precedence and Expires is ignored.

Should I use Expires or Cache-Control?

Use Cache-Control: max-age for new work, because it is a relative number of seconds and takes precedence. Keep Expires only if an older tool or audit still checks for it, and make sure both give the same lifetime. A mismatch is resolved in favour of max-age, which can surprise you.

What value should I use for the Expires header?

For files whose URL changes with their content, use a lifetime of one year: Cache-Control: max-age=31536000, immutable. For HTML and files with a fixed URL, use no-cache with an ETag so the browser revalidates. An Expires date in the past or the value 0 marks the response as already stale.

How do I add Expires headers in nginx or Apache?

In nginx, use expires 1y; inside a location for static files; it sets Expires and Cache-Control: max-age. In Apache, enable mod_expires, set ExpiresActive On and add ExpiresByType rules such as "access plus 1 years". To add immutable, set Cache-Control yourself with add_header in nginx or mod_headers in Apache.

How do I check whether the header is working?

Run curl -sI on the file URL and read cache-control, expires and etag. Then send the ETag back with If-None-Match and expect 304 Not Modified. In browser developer tools the Network panel shows "(disk cache)" for files served from cache.

Sources

Filed underAnalytics

Was this article helpful?

We use your answers to fix and expand this page.

Lin Zhou

Product Experience Specialist

Writes about product experience, practical workflows, and growth, turning complex processes into clear steps for everyday decisions.

See whether faster pages reach real visitors

Real-user Core Web Vitals by page, with the slowest pages listed and date annotations to mark a deploy.

See your data clearly. Find your growth.

Every click, backed by data. Install one line of code and see your first numbers in a minute.

No credit card · Free plans for analytics and chat · Cookieless analytics