---
title: "Faster Astro builds on Cloudflare Pages, without stale pages"
description: "My 3,000-page Astro site rebuilt everything on each Cloudflare Pages deploy. Build cache, a safe cacheKey and Node 26 took it from 6:08 to 1:27 in my tests."
date: 2026-12-04
categories: ["web-development"]
tags: ["astro","cloudflare","performance"]
---

Turn on the build cache in your Cloudflare Pages project (Settings → Build → Build cache). In my tests on this 3,000-page Astro site, that one switch cut a full build from 5 min 27 s to 1 min 27 s. Pages then keeps `node_modules/.astro` between deploys, the folder where Astro stores its image cache, its content store and its incremental page cache. With the cache off, every deploy is a cold build.

Mine was off. bitdoze.com has 1,026 posts across English, Spanish and German, and every push re-encoded 11,559 images from scratch. That cold build took 6 min 8 s on my 8-core machine before any of the changes below. Cloudflare doesn't publish the CPU count of its Pages build machines (Workers Builds gets 2 to 4 vCPUs), so it's likely slower there.

Flipping the switch wasn't the whole job, though. The site also runs Astro's `experimental.incrementalBuild`, and as soon as Pages started restoring that cache, my cache key would have served stale pages. I proved it with a title change before I fixed it. Below is everything I measured and changed, plus one experimental flag I switched off again.

In July I [benchmarked Astro 6 against Astro 7](/astro-7-faster-builds/) on this same site. That one was about the compiler. This one is about the setup around it.

## Where does an Astro build spend its time?

On a cold build of this site, encoding images takes 3 min 56 s of the 6 min 8 s total, or 64%. Page rendering (58 s) and the Vite bundle (45 s) come next.

I measured it the way Cloudflare builds. I cloned the repository into an empty folder, ran `bun install --frozen-lockfile` and timed every step of the build script on its own. A fresh clone is what Cloudflare Pages builds with the cache off. A second build in the same folder, with `dist/` deleted and `node_modules/.astro` kept, is what Pages builds with the cache on.

| Phase | Cache off (cold) | Cache on (warm) |
|---|---|---|
| Content sync and types | 3.7 s | 2.5 s |
| Vite bundle (MDX, components, CSS) | 44.8 s | 45.3 s |
| Rendering 2,999 pages | 58.4 s | 25.0 s |
| Optimized images | 3 min 56 s | 2.9 s |
| Validators and Pagefind | 21.1 s | 20.3 s |
| **Total** | **6 min 8 s** | **1 min 41 s** |

![Bar chart of bitdoze.com build phases with the Cloudflare Pages cache off and on: optimized images take 3 min 56 s cold and 2.9 s warm, page rendering 58.4 s and 25.0 s, the Vite bundle about 45 s both times](../../assets/images/26/12/faster-astro-builds-cloudflare-pages-build-phases.webp)

*Astro 7.4.11 on Node 24, 8 cores, December 4, 2026. The warm build reused `node_modules/.astro` from the cold one.*

Astro turns each source image into several WebP widths for `srcset`, plus a 1200 px JPEG for every post's `og:image`. On this site that adds up to 11,559 files. The Vite bundle doesn't care about the cache at all, because it compiles every MDX file on every build. Page rendering drops on the warm run only because 1,026 post pages came back from the incremental cache, which is where the trouble started.

You don't need a script to get this breakdown. The build log already has a timestamped line for each phase, and since Astro 7.4 `astro build --timings` prints a summary of where the time went.

## How do you turn on the Cloudflare Pages build cache?

Open the project under Workers & Pages and go to Settings → Build → Build cache → Enable. From then on, Pages saves `node_modules/.astro` after each build and restores it before the next one.

The [Pages build caching docs](https://developers.cloudflare.com/pages/configuration/build-caching/) list that one directory for Astro. On this site it holds 518 MB:

| What | Path | Size |
|---|---|---|
| Image cache (10,499 files) | `node_modules/.astro/assets/` | 242 MB |
| Copies of skipped pages | `node_modules/.astro/dist/` | 205 MB |
| Incremental build manifest | `incremental-build.json` | 39 MB |
| Content layer store | `data-store.json` | 32 MB |

Pages also keeps Bun's global install cache (`.bun/install/cache`), so `bun install` doesn't download every package again.

After you enable it:

1. Push a commit. That first build fills the cache, so it's still a cold build.
2. Open the log of the build after that. Post pages under `generating static routes` should end in `(restored)`, and image lines under `generating optimized images` should end in `(reused cache entry)`.

Each project gets 10 GB, and Pages purges the cache seven days after it was last read, so a site that deploys at least weekly keeps it warm. If a deploy ever looks wrong, the Clear Cache button in the same panel resets it. If your site isn't on Pages yet, [deploying an Astro blog to Cloudflare Pages](/deploy-astrojs-cloudflare/) covers the first setup.

**Fix your cacheKey before you enable the cache**

If you use `experimental.incrementalBuild`, the cache brings back whole pages, not only images. Deploy the cache key from the next section first and turn the cache on after. Otherwise the first warm deploys can restore pages with old related posts and footers.

## Why did incremental builds serve stale pages?

Because my cache key only described the post itself, while every post page also shows data from other posts. Astro re-renders a page when its key or its imported code changes, not when another entry it displays changes.

`experimental.incrementalBuild` arrived in Astro 7.2. A page from `getStaticPaths()` that returns a `cacheKey` is skipped when the key and a hash of every module the page imports both match the previous build. Astro copies the old HTML from `node_modules/.astro/dist/` instead of rendering it again.

My post route used each post's own dates as the key:

```ts title="src/pages/[...slug].astro (before)"
cacheKey: (post.data.lastmod ?? post.data.date)?.toISOString?.() ?? getEntrySlug(post),
```

It looked safe to me for two months. The module hash catches every change to a layout, a component or a utility. It knows nothing about the data a page reads from other entries, and a post page on this site reads plenty:

- the related post cards, with each post's title, cover and reading time
- the footer's three latest posts
- hreflang links and the language switcher, built from the post's [translated twins](/astro-i18n-localization/)
- tag chips, which only link tags that have 3 or more posts
- series navigation and the next-step card
- the author box, from the authors collection
- the copyright year, and two promo boxes with end dates

So I tested it. I changed the title of the newest post to a marker string and rebuilt with the old key. 1,025 of the 1,026 post pages came back `(restored)`. `/blog/` showed the new title, because listing pages have no cache key and always render. `/linux-commands/` still showed the old title in its footer.

None of this reached production, and only by luck. With the Pages cache off, every build was a full one. Turning the cache on is what would have exposed it.

### The cache key I use now

The key hashes every published post (frontmatter and body), every author and the current year:

```ts title="src/pages/[...slug].astro"
import { createHash } from "node:crypto";
import { getCollection } from "astro:content";

export async function getStaticPaths() {
  const posts = await getCollection("posts", ({ data }) => !data.draft);
  const authors = await getCollection("authors");
  const siteKey = createHash("sha256")
    .update(JSON.stringify(posts.map((p) => [p.id, p.data, p.body])))
    .update(JSON.stringify(authors.map((a) => [a.id, a.data])))
    .update(String(new Date().getFullYear()))
    .digest("hex")
    .slice(0, 16);

  return posts.map((post) => ({
    params: { slug: post.id },
    props: { post },
    cacheKey: siteKey,
  }));
}
```

That's the same hash my route uses. The real one goes through my cached collection helpers and appends two promo flags per post, like `$:01`. That way a promo that ends on a date still re-renders the posts that showed it. Drop the authors line if you don't have an authors collection.

I checked it three ways in a throwaway copy of the repository:

| Change in the commit | Post pages reused | Result |
|---|---|---|
| One news digest edited | 1,026 of 1,026 | every file matched an `astro build --force` rebuild, apart from the RSS build time |
| One post title changed | 0 | matched `--force`, new title in every footer |
| A link added to an old post's body | 0 | the new link is on the page |

The key is coarse on purpose. Any post edit re-renders every post page, which costs about 35 seconds on my machine. A precise key would have to know which posts each page shows, so I'd be copying the related-posts ranking into `getStaticPaths()` and keeping two copies of it in sync. And whenever I publish, I add links to older posts anyway, so most content pushes touch several posts and would re-render most pages regardless. The win comes from every other push: the daily news digest, or an edit to a page the post route doesn't import. Those reuse all 1,026 post pages.

The [Astro incremental build docs](https://docs.astro.build/en/reference/experimental-flags/incremental-build/) list two more limits:

- Editing middleware does not invalidate cached pages, so run `astro build --force` after you change it.
- Pages that render server islands re-render on every build unless `ASTRO_KEY` stays the same.

## Drop image widths you don't need

Two of my breakpoints sat 18 pixels apart, and dropping one removed 1,675 image files from every cold build. Both were in `astro.config.mjs`:

```js title="astro.config.mjs"
image: {
  layout: "constrained",
  breakpoints: [480, 750, 768, 1024, 1440],
},
```

With the `constrained` layout, Astro's `getWidths()` takes the requested width, twice that width and every breakpoint, then drops anything wider than double the width or the original file. Any image at least 768 px wide got both files. The post cover had the same pair in its own `widths=`.

You can spot pairs like this on your own site after a build:

```bash
grep -rhoE --include=*.html 'srcset="[^"]*"' dist | tr ',' '\n' | grep -oE ' [0-9]+w' | sort | uniq -c | sort -rn | head
```

On mine, `750w` showed up 4,509 times and `768w` 4,491 times. I removed 750 from both lists:

```diff title="astro.config.mjs"
-    breakpoints: [480, 750, 768, 1024, 1440],
+    breakpoints: [480, 768, 1024, 1440],
```

```diff title="src/layouts/PostLayout.astro"
-  widths={[480, 750, 768, 1024, 1280]}
+  widths={[480, 768, 1024, 1280]}
```

A cold build now encodes 9,884 images instead of 11,559, and `dist` went from 17,257 to 15,582 files. The only HTML change was the missing `750w` entry in each `srcset`. A phone that would have picked the 750 px file now downloads the 768 px one, about 5% more pixels for that image.

The file count turned out to matter more than the build time. Cloudflare Pages Free stops at 20,000 files per site (paid plans go to 100,000 once you set `PAGES_WRANGLER_MAJOR_VERSION=4`), and I was at 86% of it. Every new post adds its own HTML page, a Markdown copy and a handful of image files.

## Which Node version builds Astro fastest?

Node 26. On the same site it ran `astro build` in 104.7 s, against 114.5 s on Node 24 and 124.6 s on Node 22.

Cloudflare's v3 build image defaults to Node 22.23.4 and reads `.nvmrc` or `.node-version` if you commit one. My `.nvmrc` said `22`, so that's what Cloudflare used. I ran the same build on three versions (Astro 7.4.11, images cached, every page rendered):

| Node | `astro build` | Vite bundle | Page rendering | Peak memory |
|---|---|---|---|---|
| 22.23.5 | 124.6 s | 45.1 s | 66.0 s | 5.5 GB |
| 24.17.0 | 114.5 s | 44.4 s | 59.7 s | 5.4 GB |
| 26.14.0 | 104.7 s | 41.7 s | 52.7 s | 5.1 GB |

That's one run per version, so don't read much into the last digit. Two runs on Node 24 came out 3 seconds apart, and the 20-second gap between Node 22 and Node 26 is well outside that noise.

```bash
echo 26 > .nvmrc
```

Then look under Settings → Variables and Secrets. I had an old `NODE_VERSION=22` there, and Cloudflare's docs don't say whether the variable or the file wins when both exist. I deleted the variable so the version lives in one place. Astro 7.4.13 needs Node 22.23.4 or newer, so all three versions above work. On your own machine, [nvm installs Node 26](/install-nodejs-using-nvm-macos-ubuntu/) next to the versions you already have.

## Update dependencies with a 7-day cooldown

Several packages had releases only 1 to 6 days old when I upgraded, Astro 7.4.14 among them. A week's wait gives security researchers time to flag a hijacked release before it lands in my lockfile. Bun has a flag for this, and it applies to transitive dependencies too:

```bash
bun add --minimum-release-age=604800 astro@^7.4.13 @astrojs/mdx@^8.1.6 tailwindcss@^4.3.5 @tailwindcss/vite@^4.3.5 satori@^0.63.2
bun add -d --minimum-release-age=604800 pagefind@^1.6.0 eslint@^10.15.0 prettier@^3.10.1 typescript-eslint@^8.74.0
```

`604800` is seven days in seconds. It held back eight packages, and of the 84 that changed in `bun.lock`, the newest was 7.7 days old at install time. The npm side of the same idea (`min-release-age` in `.npmrc`, `ncu --cooldown`) is in [how to update all Node.js dependencies](/nodejs-update-dependencies/).

TypeScript stayed on 6.0.3. typescript-eslint 8.74 accepts TypeScript `<6.1.0`, and `@astrojs/check` 0.9.12 accepts `^5.0.0 || ^6.0.0`, so TypeScript 7 would break both `bun run lint` and `bun run check`.

### Diff the build output after every upgrade

I compared every file in `dist` from before and after the upgrade. The HTML changed in three places:

- the generator meta tag, which now says 7.4.13
- the stylesheet's hashed file name, after Tailwind 4.3.5
- syntax colors on 13 code-heavy pages, where hex colors inside CSS blocks lost the pink italic "invalid" style

The real regression was a PNG. `satori` went from 0.33.5 to 0.63.2, and `/og-default.png` came out with its subtitle gone and the title pushed down. satori 0.63 sizes boxes the way a browser does by default. The 80 px of padding came on top of `width: 100%` and `height: 100%`, so the layout grew past the 630 px canvas and the subtitle landed outside the image. One line fixed it:

```ts title="src/utils/og-image.ts"
style: {
  display: "flex",
  flexDirection: "column",
  justifyContent: "space-between",
  width: "100%",
  height: "100%",
  boxSizing: "border-box",
  padding: "80px",
},
```

No validator fails on an image that renders wrong. I only caught it because I opened the before and after PNGs next to each other.

## Does parallel prerendering make Astro builds faster?

A little, at a high memory cost. With 7 workers, page rendering went from 57.7 s to 48.3 s and peak memory from 5.2 GB to 12.7 GB, so I left `experimental.parallelPrerender` off.

Astro 7.4 added the flag. It renders pages in worker threads, by default on every CPU core but one. The 58-second rendering phase looked like an easy target, so I tried it with 7 workers on 8 cores:

| | One thread | `parallelPrerender`, 7 workers |
|---|---|---|
| Page rendering | 57.7 s | 48.3 s |
| Peak memory | 5.2 GB | 12.7 GB |
| Files in `dist` | 15,878 | 16,329 |

The HTML came out byte-identical, which is good news for the feature. It was still a bad trade here. Rendering got 16% faster, memory use grew 2.4 times and the build shipped 451 extra files. Those were original-size copies of images (42 MB) that no page referenced and that the single-thread build leaves out. Cloudflare publishes 8 GB of memory for Workers Builds and no figure at all for Pages. A 12.7 GB build isn't one I want to test against that limit on deploy day.

For `build.concurrency`, Node heap flags and caching API calls inside `getStaticPaths()`, the [general Astro build optimization steps](/astro-ssg-build-optimization/) still apply. They pay off more on sites that fetch data at build time than on a Markdown blog like this one.

## Before and after

Here's the whole change in one table, measured on my 8-core machine with the same steps Cloudflare runs.

| | Before | After |
|---|---|---|
| Astro | 7.4.11 | 7.4.13 |
| Node | 24 locally, 22 on Cloudflare | 26 |
| Cold build (cache off) | 6 min 8 s | 5 min 27 s |
| Warm build (cache on) | 1 min 41 s | 1 min 27 s |
| Images encoded on a cold build | 11,559 | 9,884 |
| Files in `dist` | 17,257 | 15,582 |
| Post pages restored after a title edit | 1,025, with stale footers | 0, all re-rendered |

The cache is still the big lever. On the same code it takes the full build from 5 min 27 s to 1 min 27 s. Node 26, the dropped image width and the updates took another 41 seconds off a cold build.

The Pages settings I ended up with:

| Setting | Value |
|---|---|
| Build command | `bun run build` |
| Build output directory | `dist` |
| Build system version | Version 3 |
| Build cache | Enabled |
| Node.js | `.nvmrc` with `26`, no `NODE_VERSION` variable |
| Build watch paths | `*` |

I also checked whether excluding `docs/` from the watch paths would skip builds. None of the 276 commits since September changed only docs, so it would have skipped nothing, and I left it at `*`. If you run Bun on Pages, [the Bun migration guide](/migrate-astro-bun/) covers `BUN_VERSION` and the install step.

## FAQ

**Does Cloudflare Pages cache Astro's build output?**

Only with Build cache enabled under Settings → Build. Pages then saves and restores `node_modules/.astro`, which holds Astro's image cache, the content layer store and the incremental page cache. On this site that's 518 MB, against a 10 GB limit per project.

**What cacheKey should an Astro blog use?**

A hash of everything the page shows that isn't code, since Astro already hashes the components and layouts a page imports. For a post page that means every post's frontmatter and body plus the authors, because related cards and footers show other posts. A per-post date key is only safe when a page never displays another entry.

**How long does Cloudflare keep the Pages build cache?**

Seven days after the last build that read it. Each project gets 10 GB, and when a project goes over, Pages deletes the entries read least recently. A site that deploys at least once a week keeps its cache.

**How do I see which part of an Astro build is slow?**

Run `astro build --timings` on Astro 7.4 or later, or read the timestamps on the `generating static routes` and `generating optimized images` lines in the build log. On a cold build of this site, images took 3 min 56 s of the 6 min 8 s.

**Do I need to clear the build cache after upgrading Astro?**

No. Astro throws away its incremental page cache by itself when the lockfile or the Astro config changes, so the first build after an upgrade re-renders every page. Use Clear Cache in the Pages build settings only when a deploy looks wrong.