Migrating Next.js Monorepos to Cloudflare Workers: Performance and Cost Optimization
These articles are AI-generated summaries. Please check the original sources for full details.
Deploying a Next.js Monorepo to Cloudflare Workers: Lessons from the Trenches
Engineering lead Lewis Kori migrated a production monorepo containing three Next.js apps from Firebase to Cloudflare Workers. The move replaced a fragile GitHub Actions pipeline with a git-integrated workflow that reduced costs to a $5 monthly flat fee for the dashboard app.
Why This Matters
While managed platforms like Firebase offer ease of use, they often lag behind framework updates like Next.js App Router and Server Actions, creating technical debt. Moving to Cloudflare Workers resolves this by utilizing @opennextjs/cloudflare to adapt builds, though it requires managing specific runtime constraints like the 3 MiB free-tier bundle limit and Node.js API incompatibilities.
Key Insights
- Cloudflare’s $5/month paid plan increases the worker bundle limit from 3 MiB to 10 MiB, essential for apps using Sentry or heavy i18n libraries (Kori, 2026).
- Runtime environment variables in Cloudflare Workers require a compatibility_date of 2025-04-01 or later to automatically populate process.env (Wrangler Config, 2025).
- @opennextjs/cloudflare serves as the bridge for Next.js on Workers, used to manage Nx 22 monorepos with pnpm workspaces.
- The Next.js Adapter API, stabilized in version 16.2, provides a versioned contract for platforms like Cloudflare to consume routes and assets without reverse-engineering build outputs.
Working Examples
Standard wrangler.jsonc configuration for @opennextjs/cloudflare deployments.
{
"$schema": "node_modules/wrangler/config-schema.json",
"main": ".open-next/worker.js",
"name": "my-app",
"compatibility_date": "2025-04-01",
"compatibility_flags": [
"nodejs_compat",
"global_fetch_strictly_public"
],
"assets": {
"directory": ".open-next/assets",
"binding": "ASSETS"
}
}
Correct method for accessing cookies in Cloudflare Workers middleware, avoiding Node.js-specific next/headers.
export async function middleware(req: NextRequest) {
const token = req.cookies.get('access_token')?.value;
}
Practical Applications
- Use Case: Protecting preview environments using Cloudflare Zero Trust Access to gate staging URLs behind identity providers. Pitfall: Hardcoding secret defaults in Zod schemas can lead to silent failures; use runtime guards instead.
- Use Case: Deploying multiple environments via Wrangler environments and ‘env’ blocks in wrangler.jsonc. Pitfall: Forgetting that ‘vars’ and ‘services’ keys are non-inheritable in Wrangler, resulting in empty variables for staging workers.
References:
Continue reading
Next article
How to Deploy OpenShift IPI on KVM Using Redfish and Sushy Simulation
Related Content
Solved: The Ultimate WordPress Pagespeed Guide
This guide details WordPress performance optimization, achieving peak performance through server tuning, caching, and infrastructure scaling, resulting in faster load times.
Optimizing Cloudflare Cache Rates: Fixing Astro SSR Headers with Nginx Map
Learn how an Nginx map directive increased Cloudflare cache rates from 1.1% to 47.3% by overriding Astro Node adapter defaults.
Top 10 Next.js Monitoring Tools for 2026: Technical Review and Comparison
A technical comparison of 10 Next.js monitoring tools, evaluating performance metrics and the impact of the stabilized instrumentation.ts hook.