Next.js is designed by Vercel to run seamlessly on their proprietary edge and serverless infrastructure, or in a self-hosted Node.js Docker container.
When building MergedCode, I wanted a setup that combined sub-50ms global latency with near-zero idle compute costs and automated global replication. That led me to deploy Next.js onto Cloudflare Workers using OpenNext.
Here is what works beautifully, what caught me off guard, and the hard-won architectural patterns you need to know.
Why Cloudflare Workers?
In a traditional container deployment (such as Google Cloud Run or AWS ECS), you pay for cold container boots (often 1–3 seconds) or pay continuous idle CPU fees to keep instances warm.
Cloudflare Workers operate on V8 isolates rather than Linux containers:
- Instant cold starts: Cold starts typically resolve in under 10–20 milliseconds.
- Global distribution: Code runs across 300+ edge locations worldwide right next to the user.
- Zero idle fees: You only pay for exact request execution time.
How OpenNext Bridges the Gap
Next.js relies heavily on Node.js standard libraries (node:fs, node:crypto, node:http, async local storage). Cloudflare Workers run inside the V8 JavaScript engine with the workerd runtime, which does not provide a complete Node.js operating system environment.
OpenNext acts as a build-time compiler:
- It analyzes Next.js output artifacts from
.next/standalone. - It bundles required Node.js polyfills (unenv).
- It separates static assets from server chunks.
- It outputs a single clean worker script alongside static asset manifests ready for Cloudflare Pages or Workers Static Assets.
The Real-World Challenges
While the benefits are massive, deploying full-stack Next.js on Cloudflare Workers requires navigating a few non-obvious realities.
1. Node.js Polyfill Limits
Not all Node.js native C++ modules or low-level APIs work in V8 isolates:
- Traditional database drivers that rely on raw TCP sockets (like standard
pgwithout connection pools) need HTTP or WebSocket connection adapters (such as@neondatabase/serverlessor Cloudflare Hyperdrive). - File system access (
fs.readFileSync) only works during build time. At runtime, dynamic asset reads must use Cloudflare KV or R2 buckets.
2. Static Asset Storage with Cloudflare R2
Next.js projects contain client JavaScript bundles, CSS files, images, and fonts. Storing multi-megabyte static bundles directly inside your Worker script exceeds Cloudflare’s script size limits.
The solution is offloading all assets to Cloudflare R2 (S3-compatible zero-egress object storage):
- During CI/CD, static files (
.next/static/) upload directly to an R2 bucket. - A custom Cache-Control header (
public, max-age=31536000, immutable) ensures assets are cached indefinitely on Cloudflare's global CDN edge. - The Worker only handles dynamic SSR, React Server Components (RSC), and API route handlers.
3. Image Optimization at the Edge
Next.js's default image optimization uses Sharp, a native binary that does not run directly inside standard V8 isolates.
You have three options:
- Disable image optimization (
images: { unoptimized: true }innext.config.js) if you already host pre-scaled assets. - Cloudflare Images: Delegate dynamic resizing and WebP/AVIF transformation to Cloudflare's edge image resizing service.
- External CDN: Use Cloudinary or Imgix for user-uploaded avatars and screenshots.
Deployment Checklist for OpenNext
If you're moving a Next.js project to Cloudflare Workers, follow this sequence:
- Verify your dependencies: Ensure your database and authentication libraries support edge runtimes (e.g., Supabase, Neon, Prisma Accelerate, or standard REST/GraphQL).
- Configure
wrangler.toml: Enable compatibility flags:compatibility_date = "2024-09-23" compatibility_flags = ["nodejs_compat"] - Use streaming responses: Next.js App Router streaming (
Suspense) works out of the box on Cloudflare Workers, allowing you to flush the HTML shell to the browser immediately while asynchronous database queries complete.
The Verdict
Deploying Next.js to Cloudflare Workers with OpenNext has transformed how I ship web applications. You get the developer ergonomics of the App Router, instant global distribution, and unbeatable cost efficiency. It requires understanding the boundaries of V8 isolates, but once configured, you never look back.