A CDN (content delivery network) stores copies of your Astro site’s files on servers in many locations and answers each visitor from the one closest to them. For a static Astro site, the CDN is effectively the whole host: every HTML page, stylesheet, script and image is a file at the edge, so pages load quickly worldwide and traffic spikes don’t need extra servers. Hosts like Cloudflare Pages, Netlify and Vercel put an Astro CDN in front of your site automatically.
This explainer covers what a CDN actually does, why Astro sites benefit so much, how on-demand pages use one, and when to configure a CDN yourself.
Key Takeaways
- A CDN serves files from edge locations near visitors, cutting the network distance on every request.
- Static Astro output is ideal CDN content: every page is a file that can be cached.
- CDNs also handle HTTPS, compression, HTTP/2 and HTTP/3, and absorb traffic spikes and attacks.
- On-demand Astro pages can be cached at the CDN with
s-maxage, or run at the edge with an edge adapter.build.assetsPrefixserves the/_astro/folder from a separate CDN domain if you need one.
What a CDN is
Without a CDN, every request travels to one origin server. A visitor in Sydney requesting a site hosted in Frankfurt waits for the request to cross the world and back, several times over for the HTML, CSS, scripts and images. That round-trip time is set by physics, and no amount of code optimization removes it.
A CDN puts servers, called points of presence or edge locations, in many cities. When a visitor requests a file:
- DNS or anycast routing sends the request to the nearest edge location.
- If the edge has a cached copy, it answers immediately. That’s a cache hit.
- If not, the edge fetches the file from the origin, returns it, and stores a copy for the next visitor. That’s a cache miss.
For a static site deployed to a CDN-backed host, step 3 rarely happens, because deploys push files to the network ahead of time.
What a CDN does for an Astro site
Lower latency
The edge is close to the visitor, so each request completes faster. This matters most for time to first byte (TTFB) and, through it, for largest contentful paint. It’s why a static Astro site often loads as quickly in another continent as it does next to the origin. The Core Web Vitals guide explains how TTFB feeds into LCP.
Caching with hashed assets
Astro writes processed CSS, JavaScript and images to dist/_astro/ with content hashes in their names. A CDN can cache those files indefinitely, and browsers can too, because a new deploy produces new names. HTML keeps its URLs, so the CDN purges it on each deploy. The Astro caching explainer covers the headers.
HTTPS, compression and modern protocols
Edge servers terminate TLS with automatic certificates, compress text files with Brotli or gzip, and speak HTTP/2 and HTTP/3, which multiplex many requests over one connection. You get all of that without configuring a web server.
Capacity and protection
Traffic spreads across many edge servers, so a launch, a front-page link or a bot surge doesn’t overload anything. Most CDNs also filter attack traffic and offer firewall rules and rate limiting.
Edge features
Many CDNs can run redirects, header rules and small functions at the edge, such as _redirects and _headers files on Cloudflare Pages and Netlify, or edge middleware. That covers things a static site can’t do in HTML alone.
Why static Astro output is ideal CDN content
A CDN works best when responses are identical for everyone and change only on deploy. That describes a default Astro build exactly:
| Astro output | CDN behavior |
|---|---|
| Prerendered HTML pages | Cached at the edge, purged on each deploy |
/_astro/ CSS, JS, images |
Cached long term (hashed names) |
Files from public/ |
Cached; purged on deploy |
| Client islands | JavaScript files served from the edge like any asset |
There’s no origin server doing work per request at all. The “origin” is just the deploy storage. That’s the main reason static Astro hosting is fast and often free. The deploy guide compares the CDN-backed hosts.
How on-demand Astro pages use a CDN
Pages rendered per request with an adapter run code, so the CDN can’t just hold a file. There are three ways to keep them fast.
Cache the response at the edge. If a page is the same for many visitors, send a shared-cache header and the CDN stores the rendered HTML:
---
// src/pages/events/index.astro
export const prerender = false;
const events = await getUpcomingEvents();
Astro.response.headers.set('Cache-Control', 'public, max-age=0, s-maxage=120, stale-while-revalidate=600');
---
<ul>{events.map((e) => <li>{e.title}</li>)}</ul>
The first request renders the page; the next two minutes of requests in that region are cache hits. stale-while-revalidate lets the CDN refresh in the background.
Run the code at the edge. With the Cloudflare adapter, Astro runs inside Workers on the CDN’s own servers, so even uncached renders happen near the visitor. Netlify and Vercel offer edge runtimes for middleware and functions too. The Astro adapters explainer compares the options.
Keep most of the page static. Server islands let the page itself be a cached static file while a single component renders per request. The server islands guide shows the pattern.
Personal pages, like dashboards and carts, should never be cached publicly. Mark them private, no-store.
Using a separate CDN for Astro assets
Most sites never need this, because the host’s CDN serves everything. But if your HTML is served from one place and you want assets from a dedicated CDN domain (common in larger setups), set build.assetsPrefix:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://www.example.com',
build: {
assetsPrefix: 'https://cdn.example.com',
},
});
Astro then writes every /_astro/ URL in your HTML as https://cdn.example.com/_astro/.... Upload the dist/_astro/ folder to that CDN as part of your deploy. You can also pass an object with per-file-type prefixes, such as a different domain for images. Files in public/ aren’t prefixed.
Image CDNs
Image CDNs are a specialized type: they store originals and resize, crop and convert formats on request through URL parameters. Astro optimizes local images at build time with <Image />, so a static site usually doesn’t need one. They become useful when images come from a CMS, when there are tens of thousands of them, or when on-demand pages would otherwise transform images at request time. Several adapters can route <Image /> through the host’s image service; CMSs like Sanity and Storyblok include an image CDN. The Image component guide covers remote image setup.
Do you need to configure a CDN yourself?
| Hosting setup | CDN situation |
|---|---|
| Cloudflare Pages, Netlify, Vercel | Built in; nothing to configure |
| GitHub Pages | Served through GitHub’s CDN; limited header control |
| AWS S3 | Add CloudFront in front of the bucket |
| Node.js server or VPS | Put a CDN (for example Cloudflare’s proxy) in front, and set cache headers |
| Shared hosting | Often none; a proxy CDN in front helps a lot |
If you’re self-hosting with the Node adapter, a CDN in front is the single biggest speed improvement you can make: static assets and cacheable pages stop reaching your server entirely.
Every Komal theme builds to static HTML and hashed assets, which means a deployed theme is served entirely from the CDN on any of the built-in hosts above. You can browse Astro themes by site type, and the Cloudflare Pages deploy docs walk through one CDN-backed setup. For background, MDN’s HTTP caching guide explains how shared caches like CDNs treat headers.
FAQ
What does a CDN do for a website?
It stores copies of the site’s files on servers in many locations and serves each visitor from the nearest one. That reduces latency, offloads traffic from the origin, and usually adds HTTPS, compression and protection against attacks.
Does an Astro site need a CDN?
A static Astro site deployed to Cloudflare Pages, Netlify or Vercel already uses one, so there’s nothing to add. If you self-host on a server, putting a CDN in front is strongly recommended for speed and resilience.
Can a CDN cache server-rendered Astro pages?
Yes, if the page sends a shared-cache header such as Cache-Control: public, s-maxage=120. Only cache pages whose output is the same for every visitor; personal pages should use private, no-store.
What is assetsPrefix in Astro?
build.assetsPrefix is a config option that makes Astro reference its processed assets (the /_astro/ folder) from another domain, such as a dedicated CDN. You then upload that folder to the CDN during deployment.
Is a CDN the same as web hosting?
Not exactly. Hosting stores and serves your site from an origin; a CDN distributes and caches it at the edge. Static hosts like Cloudflare Pages combine both, so the CDN effectively is the host.