---
title: "How I run Next.js 16 on Cloudflare Workers with OpenNext"
description: "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."
author: "MH TOUFIK"
published: 2026-11-16T03:00:00.000Z
updated: 2026-11-07T18:46:03.000Z
url: https://toufik.dev/blog/nextjs-16-cloudflare-workers-opennext
category: "Backend"
tags: ["Cloudflare D1", "Cloudflare R2", "Cloudflare Workers", "Next.js", "OpenNext"]
---

# How I run Next.js 16 on Cloudflare Workers with OpenNext

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:

```ts
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:

```jsonc
{
  "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:

```ts
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:

```ts
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 preview` runs the real Worker build locally. `next dev` can 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

1. **Start with a fresh Next.js app** and follow the OpenNext getting-started guide.
2. **Add `nodejs_compat`** to your Wrangler config.
3. **Add your bindings** (D1, R2, KV) one at a time, and run `wrangler types` so TypeScript knows about them.
4. **Call `initOpenNextCloudflareForDev()`** in `next.config.ts` so local dev can use them.
5. **Run `opennextjs-cloudflare preview`** and click through every page.
6. **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](https://opennext.js.org/cloudflare) — OpenNext
- [Next.js on Cloudflare Workers](https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/) — Cloudflare Docs
