Migrate Cloudflare Pages to Workers Guide

Migrate Next.js from Pages (`next-on-pages`) to the Workers-based @opennextjs/cloudflare adapter to unlock Node.js runtime, ISR, PPR, and other modern features.

OpennextjsCloudflarePagesWorkersMigration
Migrate Cloudflare Pages to Workers Guide

Why Choose OpenNext.js/Cloudflare?

Previously, we used Next.js with Cloudflare Pages for full-stack development, as it was the officially recommended solution by Cloudflare. However, Cloudflare Pages is better suited for static websites, while Next.js's latest features (e.g., ISR, PPR) require a Node.js-compatible environment. Recently, Cloudflare announced the GA of OpenNext.js/Cloudflare and recommended Pages users migrate to Workers to fully unleash the power of Next.js.

Pages Migration to Opennext Prompt
Pages Migration to Opennext Prompt

Benefits of Migration

  • Support for New Features: Runs in Cloudflare Workers with Node.js compatibility, unlocking ISR, PPR, and other features.

  • Flexible Runtime: OpenNext.js/Cloudflare is an adapter allowing Next.js to run in Node.js environments, rather than Pages' Edge.

  • Optimized Caching: Supports efficient caching via R2 or KV to boost performance.

Prerequisites

  • Next.js Runtime: OpenNext.js/Cloudflare supports only Node.js runtime (Edge is not supported).

  • Next.js Version: Supports Next.js v14 and v15.

Migration Steps

Install Dependencies

bash
Ensure wrangler version is >= 3.99.0.

Create wrangler.jsonc (Optional)

Not required, as @opennextjs/cloudflare auto-generates defaults. But if customizing (e.g., D1, R2, preview), create one:

jsonc
  • compatibility_date is required and must be >= 2024-09-23.

  • main points to build output .open-next/worker.js.

Create open-next.config.ts (Optional)

If not created, opennextjs-cloudflare build will generate a default configuration. It is recommended to create an open-next.config.ts file in the project root directory.

ts

Add .dev.vars

Create a .dev.vars file in root to support local dev/preview:

Update package.json Scripts

json

Enable Long Cache for Static Files

public/_headers:

Switch to Node.js Runtime

Remove all export const runtime = "edge".

Git Ignore Build Artifacts

Remove Cloudflare Pages Dependencies

  • Uninstall: @cloudflare/next-on-pages, eslint-plugin-next-on-pages

  • Remove setupDevPlatform() in next.config.*

  • Replace getRequestContextgetCloudflareContext from @opennextjs/cloudflare


Caching Architecture (ISR/PPR)

Serverless environments lack persistent file systems. OpenNext.js/Cloudflare uses:

  1. Incremental Cache: Stores page/data cache (R2 or KV)

  2. Queue: Manages revalidation scheduling

  3. Tag Cache: Supports revalidateTag/revalidatePath

Choose Storage Backend

  • R2: Strong consistency, suitable for long-term caching

  • KV: Low latency, ~60s multi-region consistency delay

R2 Setup Example

ts

wrangler.jsonc Bindings

jsonc

Environment Variables: Build-time vs Runtime

  • Runtime: Bindings (R2, KV, etc.) defined in wrangler.jsonc are available at runtime.

  • Build-time: These bindings are not accessible during next build.

Workarounds:

  • Shift logic to runtime via getServerSideProps or fetch

  • Use placeholder + hydrate on client

  • Or access via Cloudflare API with token

Local Dev and Verification

bash

Verification Checklist

  • compatibility_date >= 2024-09-23, wrangler >= 3.99.0

  • Removed runtime = "edge" and next-on-pages

  • name in wrangler.jsonc matches services.service

  • .open-next added to .gitignore

  • Static headers configured

  • Cache behavior works as expected

CI/CD Example (GitHub Actions)

yaml

Common Pitfalls

  • Missing version/date config

  • Edge runtime leftovers

  • Mismatched name and services.service

  • No caching backend configured (R2/KV)

  • Inconsistencies with KV

  • Incomplete cleanup (e.g., setupDevPlatform() remains)

  • Build-time binding access (should use runtime or API)

FAQ

Q: Can I mix Edge and Node routes? A: No. This adapter supports only Node.js runtime. Use Edge only for Middleware if necessary.

Q: How to debug ISR failures? A: Check DO logs (DOQueueHandler). Verify R2 access. Confirm tagCache initialization.

Q: How to enable geo caching? A: Use withRegionalCache() and fine-tune regionalCacheTtlSec.

Rollback Strategy

  • Retain last working Pages + next-on-pages deployment

  • Upload before deploy to enable safe rollout

  • Use new namespaces (R2/KV/DO) to prevent pollution

Conclusion

By following these steps, you can migrate your Next.js app from Cloudflare Pages to OpenNext.js/Cloudflare and unlock full Node.js runtime support and advanced caching. Test locally via npm run preview or deploy to production via npm run deploy.

For further assistance, refer to the official OpenNext.js and Cloudflare Workers documentation.

Recommended Posts

Responses (0)

No comments yet. Be the first to share your thoughts!