Use efficient cache lifetimes in Lighthouse: how to fix it

What the Lighthouse insight lists, what returning visitors gain from a year on your static files, and where to set it on your host, plugin or CDN.

Arjen Karel
Updated · 9 min read
Share

What does Use efficient cache lifetimes want from your server?

Use efficient cache lifetimes, called "Serve static assets with an efficient cache policy" before Lighthouse 13 and "Leverage browser caching" in older PageSpeed Insights, lists the files on your page that browsers may keep for less than 30 days. "A long cache lifetime can speed up repeat visits to your page", but your first view and your score stay the same. Static files get a year, max-age=31536000; a file that changes gets a new name.

Just want the quick fix? Jump to WordPress, Shopify, Cloudflare, Apache or nginx.

What do "Cache TTL" and "None" mean in the Lighthouse report?

Lighthouse lists every script, stylesheet, font, image and video the browser may keep for less than 30 days, from your own domain and other hosts.

Request is the file's URL. Cache TTL is the lifetime the server sent with it: "4h" is max-age=14400, four hours, Cloudflare's default when your server sends no lifetime. "None" means the server sent no lifetime at all.

Lighthouse's Use efficient cache lifetimes insight, red, Est savings of 444 KiB: thirteen of the shop's own files with Cache TTL None and Meta's fbevents.js with 20 minutes.

A WooCommerce product page with no cache lifetime on its own files and the Meta Pixel. Lighthouse 13.5, mobile, simulated throttling as in PageSpeed Insights.

"Est savings of 444 KiB" is Lighthouse's estimate of the bytes a returning visitor downloads again. Lighthouse also turns those bytes into a gain in first paint (FCP) and LCP, and when that gain is 50 ms or more the insight shows red, near the top of the list. It still has no weight in the Performance score, which comes from a load with an empty cache.

Why does a returning visitor wait for files the phone already has?

A visitor who comes back a week later can look at a white screen almost as long as on the first visit, while the stylesheets, fonts and scripts are still on the phone.

The browser stores each file with the lifetime from its Cache-Control header (max-age, in seconds). While its copy is younger than that, the browser uses it without asking the server, and the page only waits for its HTML.

Once the copy is older, the browser asks the server whether the file changed. The answer, 304 Not Modified, is a few hundred bytes but still takes a full round trip, and on a mobile connection the round trip is the slow part. The stylesheets in the head hold back the first paint until their answers are in (Render-blocking requests explains why).

Without any lifetime, Chrome guesses one from the file's age, usually a short one, and Lighthouse shows it as "None".

How much faster is a return visit when files last a year?

On a WooCommerce product page I built for an outdoor shop, I measured the first paint (FCP) on an emulated phone on Slow 4G as DevTools applies it, with four headers on the shop's own files. The first visit painted at 2.13 seconds with every header, because nothing was in the cache yet.

Header on the shop's filesNext pageA week later
A year, max-age=315360000.79 s0.78 s
Four hours (Cloudflare's default)0.80 s1.94 s
No cache header0.79 s1.94 s
no-cache1.93 s1.94 s

With every file within its lifetime, the page painted at about 0.8 seconds. On the return visit with a year, LCP came at the same moment, because the product photo was cached too.

A week later the four hours and Chrome's guess for files without a header had both expired. The 304 answers cut the download from 404 kB to about 1 kB, but the page still painted at 1.94 seconds while each stylesheet waited for its round trip. Only the year covered both the next page and the visitor who came back.

A phone screen still blank 0.8 seconds into a return visit a week later, while each file is checked with the server.

Before: no cache lifetime

The same product page complete at 0.8 seconds on a return visit a week later, every file from the browser's cache.

After: a year

The same product page on a return visit a week later, 0.8 s after the tap, Slow 4G as DevTools applies it.

Lighthouse loads your page once, with an empty cache; your field data holds every visit, returning visitors included. In CoreDash, plot LCP and TTFB (Time to First Byte) per day in Custom Timelines, once for returning visitors and once for new visitors:

CoreDash Custom Timelines for returning visitors: a line for LCP and a line for TTFB per day.

Returning visitors

CoreDash Custom Timelines for new visitors: a line for LCP and a line for TTFB per day.

New visitors

CoreDash Custom Timelines for acme-pagespeed.com, all devices, p75 per day. Each chart has its own scale.

In each chart, the space between the TTFB line and the LCP line is the wait after the first byte, which includes the wait for the files. Read it on each chart's own scale: it should be shorter for returning visitors, who already have the files in the browser's cache.

How do you set the cache lifetime for your files?

The lifetime comes from whatever serves the file: the web server, your host's settings, a caching plugin that writes server rules, or a CDN in front that overrides them. Start with the Request column: files on your own domain are yours to set, and files on another host carry that vendor's header (Files on another host).

  • Static files (CSS, JavaScript, fonts, images): a year, Cache-Control: max-age=31536000.
  • A file that changes gets a new URL: WordPress's ?ver=6.6, a hash such as app.3f9a1c.js, a new name for a new logo. The browser keeps one copy per URL, so it downloads the new one immediately.
  • CSS and JavaScript without a version in the URL: add the version first. A year on a stylesheet whose URL never changes keeps the old layout on returning visitors' phones after you change it.

Give static files that year rather than no-cache or no-store. Both also empty the list, but they make every page ask the server again or download everything (the no-cache row in the table above).

WordPress

Check your host first. Kinsta, WP Engine, WordPress VIP and WordPress.com already send a year or longer on static files. On shared hosting or your own server, a caching plugin can write the rules:

  • WP Rocket writes them into .htaccess on Apache and LiteSpeed when you activate it (CSS and JavaScript a year, images and fonts four months). On nginx it writes nothing: add the nginx block below.
  • LiteSpeed Cache has it switched off by default. Turn on Browser Cache under LiteSpeed Cache > Cache > Browser; it then sends a year.
  • W3 Total Cache has Browser Cache on by default. On nginx it writes an nginx.conf file that your server configuration has to include.

Themes and plugins put their own version number in ?ver=, so the URL changes when they update. A child theme stylesheet you edit by hand keeps its version, and returning visitors keep the old CSS. Tie the version to the file's modification time in the child theme's functions.php:

add_action( 'wp_enqueue_scripts', function () {
    wp_enqueue_style(
        'child-style',
        get_stylesheet_uri(),
        array(),
        filemtime( get_stylesheet_directory() . '/style.css' )
    );
} );

Storefront already loads the child stylesheet as storefront-child-style: use that handle instead.

WordPress gives a file you upload again a new name (photo-1.jpg), so a year is safe for uploads too.

Shopify

Theme files, the files you upload and product images already get a year, so there is nothing to set. The files that stay in the list belong to Shopify and your apps: /cdn/shopifycloud/portable-wallets/ (the dynamic checkout buttons, 5 minutes, served from your own domain so it looks like your file), shop-js (30 minutes) and the web pixels manager (1 minute). Shopify sets those headers; leave them in the list.

Cloudflare

Cloudflare gives every file a Browser Cache TTL of 4 hours, on every plan, when your server sends no lifetime or a shorter one. Set the lifetimes on your server (Apache or nginx below) and set Browser Cache TTL under Caching > Configuration to Respect Existing Headers. Or, once your CSS and JavaScript carry a version, add a Cache Rule for your static file types with Browser TTL on "Override origin" at a year. Don't raise the zone-wide Browser Cache TTL to a year: it then also covers CSS and JavaScript without a version. The other settings are in configure Cloudflare for the Core Web Vitals.

Cloudflare's Email Address Obfuscation adds email-decode.min.js to your domain with a two-day lifetime that you can't change. Switch the feature off if you don't need it (more on Cloudflare's email obfuscation).

Apache

On shared hosting this goes in the .htaccess file in the root of your site, unless a caching plugin already wrote it there. mod_expires sends both an Expires header and Cache-Control max-age:

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

Each type has to match the Content-Type your server sends (click the file in the DevTools Network panel to see it). Leave out ExpiresDefault: it also gives your HTML pages a lifetime, and visitors then see an old version of a page you changed.

nginx

On a host that runs nginx for you, ask the host: you usually can't edit the configuration yourself. On your own server, add this inside the server block:

server {
    # CSS, JavaScript, fonts and images: a changed file gets a new name
    location ~* \.(?:css|js|mjs|woff2|png|jpe?g|gif|webp|avif|svg|ico)$ {
        expires 1y;
    }
}

nginx uses the first regex location that matches, so if your configuration already has one for these extensions, put the expires line in that one. Use expires rather than add_header for this: an add_header inside a location makes nginx drop the server block's add_header lines (HSTS, CSP) for those files.

Next.js

Next.js sends public, max-age=31536000, immutable on everything under /_next/static/ and doesn't let you change it. On a Next.js site the files in the list usually come from another host, or from a CDN or proxy in front of the app that rewrites the header. Curl one file once from the app itself and once through the CDN: the header that differs points at the layer to change.

Files on another host

A file on another domain carries that vendor's header: Meta's pixel (fbevents.js, 20 minutes), the old Google Analytics analytics.js (2 hours), Hotjar (1 minute), Stripe (2 minutes). Vendors keep these short so a change on their side reaches every site quickly. Leave these files in the list, keep loader scripts such as fbevents.js on the vendor's host so they get those updates, and remove a tag nobody uses anymore.

A library or font from a public CDN is different. With an exact version in the URL (jquery@3.7.1), cdnjs, jsDelivr and code.jquery.com send a year. A version range such as swiper@11 gets 7 days on jsDelivr and 60 seconds on unpkg: pin the exact version, or host the file yourself so your own lifetime applies.

How do you see that the browser now reuses your files?

Check the header first, in the header analyzer or from the command line. Each file should now show the new max-age:

curl -sI "https://www.example.com/wp-content/themes/your-theme/style.css?ver=1.2" | grep -i cache-control

Then open the DevTools Network panel with Disable cache unticked (the settings I use) and load a page of your site. Click a file: the cache-control line under Response Headers shows whether the new lifetime reached the browser. Then go to a second page of your site. The Size column shows (disk cache) or (memory cache) for every file the browser took from its own copy, and a Status of 304 means it still asked the server.

Chrome DevTools Network panel on a return visit: every one of the shop's own files answered 304 with no bytes, each taking a round trip of about 600 ms.

Before: 304 for the shop's files

Chrome DevTools Network panel on a return visit: the HTML downloaded, every one of the shop's own files served from the disk cache in 1 to 3 ms.

After: from the disk cache

The product page's return visit a week later in the Network panel, Slow 4G as DevTools applies it.

Run Lighthouse on mobile again: what remains in the list should be files on other hosts and your platform's own files. The score stays where it was, and a score that moves a few points between runs has nothing to do with the header (why the score changes). CoreDash shows the faster return visits as they happen; Search Console takes up to 28 days.

When the header comes from a host panel you can't reach, or a CDN in front keeps rewriting what your server sends, book a Core Web Vitals power hour. I look at the headers with you and tell you which layer sets each one and where to change it; if every file left in the list belongs to a vendor, we spend the rest of the hour on your LCP element.

About the author

Arjen Karel is a web performance consultant and the creator of CoreDash, a Real User Monitoring platform that tracks Core Web Vitals data across hundreds of sites. He also built the Core Web Vitals Visualizer Chrome extension. He has helped clients achieve passing Core Web Vitals scores on over 925,000 mobile URLs.

 
Arjen Karel

Need a faster site?

I'm Arjen, and I've been fixing slow sites for 17 years. If you're running into issues you can't fix yourself, send me your URL and let's have a chat.

Send me your URL
Arjen Karel
Arjen Karel, Web performance consultant since 2009

Why isn't our fix working?

Sometimes the CDN still serves the old HTML, or the fix went into the desktop template and mobile uses another one. Send me your URL and the problem, and I reply with a few times. I check your field data first, then we spend one hour on your site, and within a day you have my notes in writing. €350 excl. VAT.

Send me your URL

No developer to build the fix? I do the work myself, on a copy of your site.

Arjen KarelCore Web Vitals still red on mobile?Send me your URL