Logo Gerardo Perrucci - Full Stack Developer

Deploy Your Next.js App to Cloudflare Workers with the Cloudflare Adapter for OpenNext

In recent years, serverless computing has revolutionized how developers deploy and scale applications. Among the myriad of platforms, Cloudflare Workers stands out for its edge-computing capabilities and global network. However, deploying modern frameworks like Next.js—which support both static site generation (SSG) and server-side rendering (SSR)—to Cloudflare Workers has historically been challenging. Enter OpenNext, a groundbreaking adapter that simplifies this process. With the release of the @opennextjs/cloudflare adapter in September 2024 , developers can now seamlessly deploy Next.js apps to Cloudflare Workers.

This article will guide you through the process, highlight key features, provide practical code examples, and explore use cases where this setup excels.

Next.js Cloudflare Workers

Why Choose Cloudflare Workers for Next.js?

The Edge Advantage

Cloudflare Workers operate at the edge, meaning your application runs closer to users worldwide. This reduces latency and improves performance significantly compared to traditional centralized hosting. For Next.js apps, which often require dynamic rendering or API routes, running on Cloudflare Workers ensures near-instant responses.

Seamless Integration with OpenNext

The @opennextjs/cloudflare adapter bridges the gap between Next.js's rich feature set and Cloudflare Workers' edge runtime. It supports:

  • Static Site Generation (SSG): Pre-rendered pages for blazing-fast load times.
  • Incremental Static Regeneration (ISR): Dynamically update static pages without rebuilding the entire app.
  • Server-Side Rendering (SSR): Render pages on-demand based on user requests.
  • API Routes: Host backend logic within the same deployment.

With the adapter reaching v1.0-beta, it is now production-ready and poised for general availability (GA) soon .


Getting Started: Deploying a Next.js App to Cloudflare Workers

Prerequisites

Before proceeding, ensure you have the following installed:

  • Node.js (v18+ recommended)
  • npm or yarn
  • A Cloudflare account with access to Workers and Pages
  • The wrangler CLI tool (npm install -g wrangler)

Step 1: Create a New Next.js App

If you're starting fresh, bootstrap a new Next.js project using the following command:

npx create-next-app@latest my-nextjs-app
cd my-nextjs-app

For existing projects, skip this step and proceed to configure the adapter.

Step 2: Install the Cloudflare Adapter for OpenNext

Install the required dependencies by running:

npm install @opennextjs/cloudflare

Update your next.config.js file to include the adapter:

// next.config.js
const { withCloudflare } = require('@opennextjs/cloudflare');

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
};

module.exports = withCloudflare(nextConfig);

This configuration tells Next.js to use the Cloudflare adapter during builds.

Step 3: Build Your Application

Run the build command to generate optimized assets:

npm run build

The adapter will output files compatible with Cloudflare Workers.

Step 4: Deploy Using Wrangler

Initialize a new Worker project if you haven't already:

wrangler init

Copy the generated files from .open-next into your Worker directory. Then, deploy your app:

wrangler publish

Your Next.js app is now live on Cloudflare Workers!


Key Features of the Cloudflare Adapter for OpenNext

Cache Handling

One standout feature of the adapter is its built-in cache handler. It intelligently manages ISR/SSG data caches, ensuring optimal performance without manual intervention . For example:

  • ISR Pages: Automatically refresh stale content after a specified interval.
  • API Responses: Cache responses at the edge for faster subsequent requests.

Local Development Support

The adapter enhances local development workflows by simulating Cloudflare bindings. You can configure these bindings via initOpenNextCloudflareForDev() options . Here's an example:

// middleware.ts
import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare';

export const onRequest = initOpenNextCloudflareForDev({
  bindings: {
    MY_BINDING: 'test-value',
  },
});

Compatibility with Next.js Versions

The adapter supports Next.js 14 and 15, making it future-proof for upcoming releases . This compatibility ensures you can leverage cutting-edge features like Turbopack and React Server Components.


Comparative Analysis: Cloudflare vs. Other Deployment Options

FeatureCloudflare WorkersVercelNetlify
Edge RuntimeYesPartial (via Edge Functions)No
Global CDNBuilt-inRequires additional setupRequires additional setup
Cost EfficiencyPay-per-request modelSubscription-basedSubscription-based
Ease of UseModerate (requires Wrangler)HighHigh

While platforms like Vercel offer seamless integrations, Cloudflare Workers excel in cost efficiency and global reach. For teams prioritizing performance and scalability, the combination of Next.js and Cloudflare Workers is unmatched.


Practical Use Cases

E-commerce Platforms

Dynamic product catalogs and personalized recommendations benefit greatly from SSR and ISR. By deploying to Cloudflare Workers, e-commerce sites achieve low-latency responses even during peak traffic.

Content-Heavy Websites

Blogs, news portals, and documentation sites can pre-render static pages using SSG while dynamically updating them via ISR. The adapter's caching mechanisms further enhance speed and reliability.

Real-Time Applications

API routes hosted on Cloudflare Workers enable real-time functionalities like chatbots or analytics dashboards. These services respond instantly due to their proximity to end-users.

Decorative quote icon

"The introduction of the Cloudflare adapter for OpenNext marks a pivotal moment in making Next.js truly portable across different hosting environments."

— OpenNext Blog

Experts agree that this innovation democratizes access to edge computing, empowering developers to build high-performance applications regardless of infrastructure constraints.

Deploying a Next.js app to Cloudflare Workers with the @opennextjs/cloudflare adapter unlocks unparalleled performance, scalability, and flexibility. Whether you're building an e-commerce platform, a content-heavy website, or a real-time application, this setup ensures your app runs efficiently at the edge.

As the adapter moves toward GA, we anticipate even more robust features and optimizations. Start experimenting today and harness the power of Cloudflare Workers for your Next.js projects.

References