How I run Next.js 16 on Cloudflare Workers with OpenNext
This site runs Next.js 16 on Cloudflare Workers with D1, R2 and KV. Here's my real OpenNext setup, the config that matters, and what surprised me.
4 min read
The page you're reading right now was built by Next.js 16 and served by a Cloudflare Worker. There's no Vercel, no VPS and no server I have to patch. When I checked the response headers from Dhaka, the page came from Cloudflare's data centre in Dhaka itself.
It didn't start this way. This site began as plain HTML, moved to a small Hono app on Workers in May 2025, and became a full Next.js app with an admin panel in August 2026. The tool that made that last step possible is OpenNext.
Here's the setup I use, the config that matters, and the things that surprised me.
The pieces
This is the whole stack behind toufik.dev:
- Next.js 16.4 with the App Router and server components
- @opennextjs/cloudflare 1.20, which turns the Next.js build into a Worker
- D1, Cloudflare's SQLite database, for posts, projects and pages
- R2 for uploaded images, and a second R2 bucket for Next.js's own cache
- KV for one tiny value that controls my page cache
- Workers rate limiting for the public forms and the admin panel
Everything is in one Cloudflare account, and the whole site deploys with one command.
The config that matters
OpenNext's own config file is almost empty. Mine only tells it to keep the Next.js cache in R2:
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
});The real work is in wrangler.jsonc. Here's a trimmed version:
{
"name": "toufikforyou-dev",
"main": "worker.ts",
"compatibility_flags": ["nodejs_compat"],
"assets": { "binding": "ASSETS", "directory": ".open-next/assets" },
"d1_databases": [{ "binding": "DB", "database_name": "toufikforyou-db" }],
"r2_buckets": [
{ "binding": "MEDIA", "bucket_name": "toufikforyou-media" },
{ "binding": "NEXT_INC_CACHE_R2_BUCKET", "bucket_name": "toufikforyou-next-cache" }
]
}Two lines are easy to miss. nodejs_compat is required, because Next.js expects Node.js APIs. And main points to my own worker.ts, not the one OpenNext generates. More on that next.
In next.config.ts, I call initOpenNextCloudflareForDev(). That one line lets next dev talk to local versions of D1, R2 and KV, so I don't need a separate dev setup.
A worker in front of Next.js
OpenNext builds a Worker that handles every request. I wrap it with a small Worker of my own, so I can do cheap things before Next.js even wakes up:
import nextHandler from "open-next-worker";
export default {
async fetch(request: Request, env: CloudflareEnv, ctx: ExecutionContext) {
const url = new URL(request.url);
if (LEGACY_HOSTS.has(url.hostname)) {
return Response.redirect(`${SITE_URL}${url.pathname}${url.search}`, 301);
}
return withEdgeCache(request, env, ctx, () => nextHandler.fetch(request, env, ctx));
},
} satisfies ExportedHandler<CloudflareEnv>;open-next-worker is just an alias in wrangler.jsonc that points to .open-next/worker.js. This is where my old domain redirects to the new one, and where public pages get an edge cache. Pages are cached in Cloudflare's cache, and when I save anything in the admin panel, the cache starts fresh. I'll write about that cache in its own post.
Images without next/image
The built-in image optimisation of next/image doesn't run on Workers out of the box. I didn't want to pay for a separate image service for a personal site, so I moved the work to upload time.
When I upload an image in the admin panel, the browser makes a sharp 800px WebP thumbnail before sending anything:
const bitmap = await createImageBitmap(file, {
resizeWidth: 800,
resizeHeight: Math.round((height / width) * 800),
resizeQuality: "high",
});Both files go to R2. A small route serves them with a one-year immutable cache header, and Cloudflare's edge cache does the rest. Simple, and very fast.
Things that surprised me
- Crypto has limits. I hash admin passwords with PBKDF2 using the Web Crypto API. Workers only allow up to 100,000 iterations, so my first version failed until I capped it.
- Preview before you deploy.
opennextjs-cloudflare previewruns the real Worker build locally.next devcan hide problems that only show up in the Worker. - Watch your bundle size. Workers have a size limit. Heavy packages you'd never think about on a normal server suddenly matter.
- Deploy from CI, not your laptop. My GitHub Actions workflow runs lint, type checks and the build first, and only deploys if all of them pass.
If you want to try it
- Start with a fresh Next.js app and follow the OpenNext getting-started guide.
- Add
nodejs_compatto your Wrangler config. - Add your bindings (D1, R2, KV) one at a time, and run
wrangler typesso TypeScript knows about them. - Call
initOpenNextCloudflareForDev()innext.config.tsso local dev can use them. - Run
opennextjs-cloudflare previewand click through every page. - Deploy from CI, and keep an eye on Workers logs for the first few days.
The bottom line
Running Next.js on Workers used to feel like a hack. With OpenNext, it feels normal. You write a regular Next.js app, and Cloudflare runs it close to your visitors, wherever they are. If you're on Vercel and the bill or the limits bother you, it's worth an afternoon to try.
Sources
- OpenNext for Cloudflare — OpenNext
- Next.js on Cloudflare Workers — Cloudflare Docs
Keep reading
All postsGo or Node.js for your mobile app's backend in 2026
Both Go and Node.js can run a solid backend for a mobile app. Here's how I'd choose between them based on the work your API actually does, not benchmarks.
4 min read
MCP for mobile developers, explained simply
MCP lets your AI agent see crash reports, Firebase data and Figma designs. Here's what it is, how Android Studio and Xcode use it, and how to stay safe.
4 min read
Kotlin Multiplatform vs Flutter vs React Native in late 2026: how to choose
All three are stable now. Here's where Kotlin Multiplatform, Flutter and React Native stand in late 2026, how they differ, and how I'd choose.
4 min read
Comments
No comments yet — start the conversation.