Site AI Auditpor Internet Solutions

Browser Caching Explained: How to Set Cache-Control Headers

14 de agosto de 20268 min de leituraVelocidade do site
Browser Caching Explained: How to Set Cache-Control Headers

Short answer: Browser caching tells visitors’ browsers to keep copies of files so they do not download them again on the next page or visit. You control it with the Cache-Control response header. A good default is a long lifetime such as max-age=31536000, immutable for versioned static files (CSS, JavaScript, fonts, images), a short lifetime or no-cache with revalidation for HTML, and private or no-store for personal or sensitive pages.

Every time a visitor opens a new page on your site, the browser needs the same logo, stylesheet, fonts and scripts it used on the previous page. Without caching instructions, it may ask the server again for each one, or download them all again. With good caching headers, those files come straight from the device’s disk or memory in milliseconds. Browser caching does not help the very first page view, but it makes every page after that, and every return visit, significantly faster.

How browser caching works

When a server sends a file, it can include headers that tell the browser how long the file stays fresh. While a cached file is fresh, the browser uses it without contacting the server at all. When it becomes stale, the browser can revalidate: it asks the server whether the file has changed, and the server answers either with the new file or with a short 304 Not Modified response that says “keep using your copy”.

Two mechanisms work together:

If a file has no caching headers at all, browsers apply their own heuristics, which are unpredictable. Speed tools flag this as “Serve static assets with an efficient cache policy” or “Use efficient cache lifetimes”.

The Cache-Control directives you actually need

DirectiveMeaningTypical use
max-age=NFresh for N secondsStatic files, short-lived pages
immutableWill never change while fresh; do not revalidateVersioned static files
no-cacheMay store, but must revalidate before each useHTML that changes often
no-storeDo not store at allSensitive pages, banking, personal data
publicShared caches (CDNs, proxies) may store itStatic files served to everyone
privateOnly the user’s browser may store itAccount pages, personalised content
s-maxage=NLifetime for shared caches onlyHTML cached at a CDN but not long in browsers
stale-while-revalidate=NServe stale copy while fetching a fresh one in the backgroundContent where a slightly old version is acceptable

The name no-cache is confusing: it does not mean “do not cache”. It means “check with the server before using”. Use no-store when you really want nothing stored. The MDN Web Docs reference on Cache-Control lists every directive in detail.

Recommended settings by file type

Cache busting: how to update long-cached files

The fear with long cache lifetimes is obvious: if the browser keeps a stylesheet for a year, how do visitors get your new design? The answer is to change the URL whenever the file changes. The browser treats a new URL as a new file.

A common pitfall is editing a theme’s CSS file directly on the server without changing its version. Visitors with a cached copy keep seeing the old styles until their cache expires. Bump the version whenever you edit an asset by hand.

How to set caching headers

  1. Check your host and CDN first. Many managed hosts and CDNs set sensible caching headers for static files automatically, and some let you configure lifetimes in a dashboard.
  2. Apache: use mod_expires and mod_headers in the server configuration or .htaccess to set Cache-Control by file type.
  3. Nginx: use location blocks matching file extensions with expires or add_header Cache-Control.
  4. WordPress: many caching plugins can write browser caching rules into .htaccess on Apache and LiteSpeed servers. On Nginx, the rules must be added to the server configuration.
  5. Applications: frameworks let you set headers per response, which is the right place for HTML and API caching decisions.

How to check your current caching

  1. Open DevTools, go to the Network panel and reload the page.
  2. Click a CSS file, an image and the HTML document, and read the cache-control response header for each.
  3. Reload again (without a hard refresh) and look at the Size column: “memory cache” or “disk cache” means the browser reused the file.
  4. Run PageSpeed Insights and review the cache lifetime audit, which lists static files with short or missing lifetimes.

Note that files from third parties, such as analytics scripts or social widgets, often have short cache lifetimes that you cannot change. Speed tools list them anyway; focus on your own files.

Browser caching vs page caching vs CDN caching

These three are often confused but solve different problems:

A fast site usually uses all three. The Cache-Control header influences both browser and CDN caches, which is why directives like public, private and s-maxage exist.

A practical example for a small business site

Imagine a typical company website on WordPress with a theme, a contact form plugin and a few images per page. A sensible caching setup would look like this. Theme and plugin CSS and JavaScript, which WordPress loads with a version string, get a one-year lifetime. Uploaded images and fonts get a long lifetime too, because new images are uploaded with new file names. HTML pages are served from a page cache on the server, but browsers are told to revalidate them, so a price change or new blog post appears on the next visit. The contact form’s confirmation page and any customer account area are marked private. With this in place, a visitor who reads three pages downloads the theme files once and afterwards only fetches the small HTML documents and any new images.

Common caching mistakes

How Site AI Audit helps

Site AI Audit checks your site from the outside, the way a browser sees it. Its speed section combines server response time, compression, page weight and the Google PageSpeed mobile results, where missing cache lifetimes show up as a cause of slow repeat loads. Each finding comes with a plain-language fix, ranked by impact together with SEO, SSL and e-mail checks. Start with a free check.

Related reading

The bottom line

Give versioned static files a one-year lifetime and mark them immutable, keep HTML short-lived or revalidated, and never let shared caches store personal pages. Change file names or version strings when files change, so long lifetimes never block an update. Then check the headers in DevTools to make sure what you configured is what visitors receive.

FAQ

How long should I cache static files?

For files whose URL changes when the content changes, one year (max-age=31536000) is the common recommendation. For files that keep the same name after updates, use a shorter lifetime such as a week. The key is to combine long lifetimes with versioned URLs.

What is the difference between no-cache and no-store?

No-cache allows the browser to store the file but requires it to check with the server before each use. No-store forbids storing the response at all. Use no-store for sensitive data and no-cache for content that changes often.

Does browser caching help first-time visitors?

No, first-time visitors have nothing cached yet. Browser caching speeds up the second page they view and any later visits. For first visits, server response time, page weight and a CDN matter more.

Why do visitors still see my old CSS after an update?

Their browser is using a cached copy that is still fresh according to its caching headers. Change the file’s version or name whenever you update it, so the browser requests the new file. WordPress does this automatically when a theme or plugin version changes.

Can I fix cache warnings for Google Analytics or other third-party scripts?

Usually not, because the third party controls those headers. Speed tools list them for completeness. Focus on your own files, and consider whether each third-party script is necessary at all.

#Caching#Page speed#Web hosting
Verifique seu próprio site — grátis.O que corrigir no seu site — e por onde começar.
Comece grátis
Internet Solutions

Mais da nossa equipe

Feitas pela Internet Solutions. Experimente nossos outros produtos — cada um economiza seu tempo de um jeito diferente.

internet-solutions.net ↗
Site AI Audit
Visão geral de privacidade

Este site usa cookies para oferecer a melhor experiência de usuário possível. As informações dos cookies ficam armazenadas no seu navegador e servem para, por exemplo, reconhecer você quando volta ao nosso site e ajudar nossa equipe a entender quais seções do site você acha mais interessantes e úteis.