Next.js 16 static export on Cloudflare Pages: _redirects, _headers and 404s that work
If you set output: "export" and keep your redirects() and headers() in next.config.ts, the build passes and none of those rules run. A static export has no server to apply them. Cloudflare Pages reads two plain-text fil
If you set output: "export" and keep your redirects() and headers() in next.config.ts, the build passes and none of those rules run. A static export has no server to apply them.
Cloudflare Pages reads two plain-text files instead: _redirects and _headers. This post covers both, plus the Node.js version trap on older Pages projects, trailing slashes, and 404 pages.
I tested everything on Next.js 16.4.6, React 19.3.0 and Wrangler 4.168.0. npx wrangler pages dev out runs Pages' routing locally, so every status code below is real output from that server. The default-header check in section 6 comes from a live *.pages.dev site.
1. The Next.js config
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "export",
images: { unoptimized: true },
// keep the rest of your config
};
export default nextConfig;
images.unoptimized is there because the default image loader needs a server. The Next.js static export docs list it as unsupported, next to redirects, rewrites, headers and proxy.
A fresh create-next-app project on 16.4 also turns on cacheComponents and partialPrefetching. With output: "export", that fails the build:
> Build error occurred
Error: Invariant: PPR cannot be enabled in export mode
Remove both options. I covered the other export errors in Next.js 16 on GitHub Pages.
2. Build settings, and the Node 18 trap
In the Pages dashboard, pick the Next.js (Static HTML Export) preset. It fills in:
| Setting | Value |
|---|---|
| Build command | npx next build |
| Build directory | out |
Next.js 16 needs Node.js 20.9 or newer. Pages projects still on the v2 build image default to Node 18.17.1, and the build stops right away. This is the real output when I ran next build on Node 18.17.1:
You are using Node.js 18.17.1. For Next.js, Node.js version ">=20.9.0" is required.
The v3 build image defaults to Node 22.23.4. On an older project, set a NODE_VERSION environment variable or add a .nvmrc file:
22.23.4
Don't rely on "engines" in package.json. Cloudflare's build image docs say v3 doesn't read it. (v2 projects move to v3 automatically on February 23, 2027.)
3. redirects() and headers() don't run
Here is what Next.js prints when they're still in the config:
โ Specified "redirects" will not automatically work with "output: export". See more info here: https://nextjs.org/docs/messages/export-no-custom-routes
โ Specified "headers" will not automatically work with "output: export". See more info here: https://nextjs.org/docs/messages/export-no-custom-routes
They are warnings. The build exits with code 0, so a CI log won't flag them. Move the rules into the two files below, and delete them from next.config.ts so nobody thinks they still apply.
4. _redirects goes in public/
Next.js copies everything in public/ to out/, and Pages reads _redirects from the root of the build output. It doesn't serve the file itself.
# public/_redirects
# Old URLs from the previous site
/old-about /about 301
/blog/old-post /blog/hello-world 301
# Moved section: keeps the rest of the path
/posts/* /blog/:splat 301
# No status code: Pages uses 302
/pricing-old /about
The format is source destination [code], one rule per line. Things to know:
- The default code is 302. Write
301for permanent moves. - Order matters: if two rules share a source, the top one wins. Put static rules before ones with
*or:placeholder. - The limits are 2,000 static and 100 dynamic redirects, and 1,000 characters per rule.
- Domain-level rules (
example.pages.dev/* https://example.com/:splat) aren't supported in this file.
Output from wrangler pages dev, using a small curl loop:
/old-about 301 http://localhost:8788/about
/posts/second-post 301 http://localhost:8788/blog/second-post
/pricing-old 302 http://localhost:8788/about
5. Match your trailing slash, or get a redirect chain
Pages has its own URL rules. It serves about.html at /about, and it redirects /about.html and /about/index.html to the clean URL.
With the default trailingSlash: false, Next.js writes out/about.html:
/about 200
/about/ 308 http://localhost:8788/about
/about.html 308 http://localhost:8788/about
With trailingSlash: true, it writes out/about/index.html, and Pages flips the direction:
/about 308 http://localhost:8788/about/
/about/ 200
Both work. The trap is a _redirects destination in the other style. With trailingSlash: true and /old-about /about 301, one old link takes two hops:
HTTP/1.1 301 Moved Permanently
Location: /about
HTTP/1.1 308 Permanent Redirect
Location: /about/
HTTP/1.1 200 OK
Write destinations exactly the way your build writes URLs. Unlike GitHub Pages, Pages doesn't need trailingSlash: true, so I leave it off.
6. _headers: cache hashed files, add security headers
On a live *.pages.dev site with no _headers file, a hashed CSS file from /_next/static/ came back with:
cache-control: public, max-age=0, must-revalidate
So the browser checks with the server on every visit. Pages answers with a 304 when the ETag matches, but it still costs a round trip. The files in /_next/static/ have content hashes in their names, and the Next.js self-hosting docs say they're safe to cache with public, max-age=31536000, immutable.
# public/_headers
# Hashed build files: safe to cache for a year
/_next/static/*
Cache-Control: public, max-age=31536000, immutable
# Every response
/*
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Result for a CSS file:
Cache-Control: public, max-age=31536000, immutable
permissions-policy: camera=(), microphone=(), geolocation=()
referrer-policy: strict-origin-when-cross-origin
x-frame-options: DENY
HTML pages keep the default public, max-age=0, must-revalidate, which is what you want, since they change on every deploy.
Watch out: when two rules set the same header, Pages joins the values with a comma. I added a catch-all /* rule with Cache-Control: public, max-age=3600 below the first block, and the CSS file got this:
Cache-Control: public, max-age=31536000, immutable, public, max-age=3600
Nothing errors, but the header now contradicts itself. Don't set Cache-Control on /*. If you need to override one path, Cloudflare's docs show how to remove a header with ! Cache-Control in a later rule, then set it again.
If your site lives on a custom domain, you can also keep the pages.dev copy out of search results:
https://:project.pages.dev/*
X-Robots-Tag: noindex
I checked it with a Host: liftoff-test.pages.dev request header: x-robots-tag: noindex came back, and plain localhost requests didn't get it. Skip this rule if pages.dev is your only domain.
Limits: 100 header rules, and 2,000 characters per line.
7. Keep 404.html in the output
Next.js writes out/404.html from app/not-found.tsx (or its built-in page). Pages finds the closest 404.html and serves it with a real 404 status:
/nope 404
/blog/nope 404
If 404.html is missing, Pages assumes a single-page app and serves the root index.html instead. I deleted the file from a copy of the output to check, and /nope came back as 200 with the home page's <h1>Home</h1>.
Every bad URL then returns your home page with status 200. You won't hit this with a plain next build, but check for it if you copy or filter out/ in a custom build step.
8. Test locally, then deploy
Before pushing, run the build through Pages' routing:
npx next build
npx wrangler pages dev out
Then curl your old URLs and a missing one:
for p in /about /old-about /posts/second-post /nope; do
printf "%-22s " "$p"
curl -s -o /dev/null -w "%{http_code} %{redirect_url}\n" "http://localhost:8788$p"
done
I also ran a Playwright check against the same server: client-side navigation to /about and /blog/hello-world, a missing post (404 with the custom page) and the /posts/* redirect. There were no 4xx responses and no console errors.
To deploy, connect the Git repo with the preset from section 2, or upload the folder with Wrangler:
npx wrangler pages deploy out
One catch: a project created with Direct Upload can't be switched to Git integration later. You'd have to create a new project.
Wrapping up
- Set
output: "export"andimages.unoptimized, and removecacheComponentsandpartialPrefetching. - Use Node 20.9 or newer on Pages (
NODE_VERSIONor.nvmrc). - Move redirects to
public/_redirectsand headers topublic/_headers. - Write
301when you mean permanent, and match your trailing-slash style. - Cache
/_next/static/*as immutable, and never setCache-Controltwice. - Make sure
404.htmlships, or every bad URL returns 200.
Are you deploying Next.js to Pages as a static export, or running it on Workers?
Disclosure
I sell Next.js Launch Skills, 13 agent skills for Claude Code, Cursor and Codex that take a Next.js site from build to launch. One of them, launch-static-deploy, covers static exports to Cloudflare Pages, GitHub Pages and Netlify, with an export checker. It's $9.99, or $5.99 with code BF40 until December 1: open it with the code applied.
The free MIT edition is on GitHub: npx skills add nandot12/nextjs-launch-skills ยท GitHub
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.