Manun Lab
3 min read
한국어로 읽기

Deploying a Next.js blog to Cloudflare Pages for free

On this page

This blog has no server. It's built with Next.js, but the build spits out a folder of plain HTML files and Cloudflare Pages serves them. Hosting costs nothing. The only money I've spent so far is $11.17 for a year of the domain.

Vercel was the first candidate. They make Next.js, so it looked like the obvious pick. But the free Hobby plan is limited to personal, non-commercial use. I want to run AdSense on this site, and the moment ads make money, that gets murky. Pro is $20 a month.

Cloudflare Pages' free plan doesn't forbid commercial use, and static traffic is effectively unlimited. The limit that matters is 500 builds a month, and a blog that only rebuilds when I publish won't come close.

The config is three lines

import type { NextConfig } from "next";
 
const nextConfig: NextConfig = {
  output: "export",
  trailingSlash: true,
  images: { unoptimized: true },
};
 
export default nextConfig;

With output: "export", next build writes static files to out/. trailingSlash turns /about into about/index.html, so URLs behave the same on any static host. Image optimization needs a server, so it has to go. I resize images myself before committing them.

Where the build broke

The config took five minutes. The first build failed a few times.

The first snag was dynamic routes. A route like posts/[slug] has to know every slug at build time. You return them from generateStaticParams and set dynamicParams = false so anything else is a 404.

export const dynamicParams = false;
 
export function generateStaticParams() {
  return getAllPosts().map((post) => ({ slug: post.slug }));
}

Then I hit a second snag. If that list is empty, say a tag route before any post uses tags, static export refuses to build. I return a single placeholder param when the list is empty and have the page call notFound() for it.

RSS and the sitemap have to end up as files too. Adding export const dynamic = "force-static" to a route handler runs it once at build time and saves the output as rss.xml.

The last one was a type error. In app/[locale]/rss.xml/route.ts I typed params as { locale: Locale }, my own "ko" | "en" union, and type checking failed the build. Next.js passes params as plain strings, so the handler has to accept { locale: string } and narrow it inside.

export async function GET(_req: Request, { params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  const locale = raw as Locale;
  // ...
}

What static export can't do

Here's what a static export can't do.

Feature With static export
Middleware (language redirects, etc.) Doesn't run
API routes that change per request Don't work. Ones built once at build time do
ISR, revalidate Don't work
cookies(), headers() Don't work
Built-in image optimization Must be turned off

Middleware is the one that actually got in my way. I wanted to send first-time visitors to /ko/ or /en/ based on their browser language, and that's normally a middleware job. I ended up doing it with a tiny script on the root page, which I wrote up in the i18n post.

Hooking up Cloudflare Pages

In the dashboard, go to Workers & Pages → Create → Pages → Connect to Git and pick the GitHub repo. These are the build settings I used:

Setting Value
Production branch main
Framework preset None
Build command npm run build
Build output directory out
Environment variable NODE_VERSION=22

There is a Next.js preset, but for a pure static export I found it clearer to leave the preset at None and fill in the command and output folder myself. NODE_VERSION keeps the build on the same Node version I use locally. Without it, Cloudflare uses its default.

After Save and Deploy, the site was up at manunlab-blog.pages.dev two or three minutes later. Every push to main redeploys it. The part I didn't expect to like this much is branch previews: pushing any other branch gets its own URL, like branch-name.manunlab-blog.pages.dev, and Cloudflare's bot comments that link on the pull request. I check design changes and new posts there before merging.

Adding the domain

I bought the domain through Cloudflare Registrar, so adding blog.manunlab.com under the project's Custom domains set up the DNS record and the SSL certificate on its own. If your domain lives somewhere else, you add a CNAME record yourself.

If you never plan to run ads, Vercel is fine. If there's any chance you'll want revenue, read the hosting terms before you start.

Manun

I write down what I learn by building things and breaking them. I build and run this blog on my own, too.

About →