The right Astro cache headers are simple: files in /_astro/ have a content hash in their name, so cache them for a year with Cache-Control: public, max-age=31536000, immutable, and serve HTML with max-age=0, must-revalidate so visitors always get the latest page. Files you put in public/ keep their names, so give them a short cache unless you version them yourself.
Key Takeaways
- Astro writes bundled JS, CSS and optimised images to
dist/_astro/with hashed filenames.- A changed file gets a new name, so long, immutable caching is safe for
/_astro/*.- HTML must revalidate, or visitors keep old pages that point at old assets.
- Files in
public/are copied as-is, without hashes; cache them briefly.
Why hashed assets are safe to cache forever
After astro build, look in dist/_astro/:
dist/_astro/
index.Bc3kq1Xz.css
Header.astro_astro_type_script_index_0_lang.D9fLp2aQ.js
hero.CJ8sXw0m_1x2b9Q.webp
The part before the extension is a hash of the file’s content. Change one line of CSS and the next build writes a new filename, and the HTML points at it. An old cached copy is never requested again, so there is no downside to caching it for a year. The folder name comes from build.assets in astro.config.mjs, which defaults to _astro.
The policy
| Path | Cache-Control |
|---|---|
/_astro/* |
public, max-age=31536000, immutable |
| HTML pages | public, max-age=0, must-revalidate |
public/ files such as /favicon.svg, /og/*.png |
public, max-age=3600 or a day |
/sitemap-index.xml, /rss.xml |
public, max-age=3600 |
immutable tells browsers not to revalidate the file even on reload. must-revalidate with max-age=0 lets the browser keep HTML but check with the server before using it, which is cheap thanks to ETag headers.
Cloudflare Pages and Netlify
Both read a _headers file from the build output. Put it in public/:
# public/_headers
/_astro/*
Cache-Control: public, max-age=31536000, immutable
/*.png
Cache-Control: public, max-age=86400
/*.svg
Cache-Control: public, max-age=86400
HTML usually already gets a revalidating policy from both hosts by default, so you only need to add the long-lived rule for assets. Check with curl after deploying rather than assuming. Our _headers file tip explains matching rules.
Vercel
{
"headers": [
{
"source": "/_astro/(.*)",
"headers": [
{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
]
}
]
}
Save this as vercel.json in the project root. The Vercel adapter also sets this header for /_astro/ when you use it.
Nginx or your own server
# /etc/nginx/conf.d/astro.conf (inside your server block)
location /_astro/ {
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
location / {
add_header Cache-Control "public, max-age=0, must-revalidate";
try_files $uri $uri/index.html =404;
}
Our Nginx tip has a full server block.
Version files in public/
Files in public/ are not processed, so /og/home.png keeps its name forever. If you need to cache one for a long time, either import it from src/ so Astro hashes it, or add a version to the URL yourself:
---
// src/components/Head.astro
import ogImage from '../assets/og-home.png';
---
<meta property="og:image" content={new URL(ogImage.src, Astro.site)} />
Importing the image gives it a hashed path under /_astro/, so the long cache rule covers it automatically.
Check what you are sending
# Terminal
curl -sI https://www.example.com/ | grep -i cache-control
curl -sI https://www.example.com/_astro/$(ls dist/_astro | grep css | head -1) | grep -i cache-control
The first should revalidate; the second should say immutable. For background on how CDNs use these headers, read what a CDN does and our caching explainer. Themes in our Astro theme catalog build to the same _astro layout, so this file works for them unchanged. The MDN Cache-Control reference covers every directive.
FAQ
Is it safe to cache Astro’s _astro folder forever?
Yes. Every file there has a content hash in its name, so a changed file gets a new URL. Cached old versions are simply never requested again.
Why do visitors still see an old version of my Astro site?
Usually the HTML is cached too long, either by a CDN rule or a max-age on pages. HTML should revalidate on every request so it always references the latest hashed assets.
Can I change the _astro folder name?
Yes, with build.assets in astro.config.mjs. Update your cache rules to match the new path.
Do images in public/ get cache-busted?
No. Astro copies public/ as-is. Import images from src/ if you want hashed filenames and long caching.