Start from CLI - scaffold a Next.js project on Workers.
npmcreatecloudflare@latest--my-next-app--framework=nextyarncreatecloudflaremy-next-app--framework=nextpnpmcreatecloudflare@latestmy-next-app--framework=nextThis is a simple getting started guide. For detailed documentation on how to use the Cloudflare OpenNext adapter, visit theOpenNext website ↗.
Next.js ↗ is aReact ↗ framework for building full stack applications.
Next.js supports Server-side and Client-side rendering, as well as Partial Prerendering which lets you combine static and dynamic components in the same route.
You can deploy your Next.js app to Cloudflare Workers using the OpenNext adapter.
Most Next.js features are supported by the Cloudflare OpenNext adapter:
| Feature | Cloudflare adapter | Notes |
|---|---|---|
| App Router | 🟢 supported | |
| Pages Router | 🟢 supported | |
| Route Handlers | 🟢 supported | |
| React Server Components | 🟢 supported | |
| Static Site Generation (SSG) | 🟢 supported | |
| Server-Side Rendering (SSR) | 🟢 supported | |
| Incremental Static Regeneration (ISR) | 🟢 supported | |
| Server Actions | 🟢 supported | |
| Response streaming | 🟢 supported | |
asynchronous work withnext/after | 🟢 supported | |
| Middleware | 🟢 supported | |
| Image optimization | 🟢 supported | Supported viaCloudflare Images |
| Partial Prerendering (PPR) | 🟢 supported | PPR is experimental in Next.js |
| Composable Caching ('use cache') | 🟢 supported | Composable Caching is experimental in Next.js |
| Node.js in Middleware | ⚪ not yet supported | Node.js middleware introduced in 15.2 are not yet supported |
Create a new project with the create-cloudflare CLI (C3).
Terminal window npmcreatecloudflare@latest--my-next-app--framework=nextTerminal window yarncreatecloudflaremy-next-app--framework=nextTerminal window pnpmcreatecloudflare@latestmy-next-app--framework=nextWhat's happening behind the scenes?
When you run this command, C3 creates a new project directory, initiatesNext.js's official setuptool ↗, andconfigures the project for Cloudflare. It then offers the option toinstantly deploy your application to Cloudflare.
Develop locally.
After creating your project, run the following command in your project directory to start a local development server.The command uses the Next.js development server. It offers the best developer experience by quickly reloading your app every time the source code is updated.
Terminal window npmrundevTerminal window yarnrundevTerminal window pnpmrundevTest and preview your site with the Cloudflare adapter.
Terminal window npmrunpreviewTerminal window yarnrunpreviewTerminal window pnpmrunpreviewWhat's the difference between dev and preview?
The command used in the previous step uses the Next.js development server,which runs in Node.js. However, your deployed application will run onCloudflare Workers, which uses the
workerdruntime. Therefore whenrunning integration tests and previewing your application, you should usethe preview command, which is more accurate to production, as it executesyour application in theworkerdruntime usingwrangler dev.Deploy your project.
You can deploy your project to a
*.workers.devsubdomain or acustom domain from your local machine or any CI/CD system (includingWorkers Builds). Use the following command to build and deploy. If you're using a CI service, be sure to update your "deploy command" accordingly.Terminal window npmrundeployTerminal window yarnrundeployTerminal window pnpmrundeployWorkers Builds requires you to configure environment variables in the"Build Variables and secrets" section.
This ensures the Next build has the necessary access to both public
NEXT_PUBLC...variables andnon-NEXT_PUBLIC_...↗, which are essential for tasks like inlining and building SSG pages.Learn more in theOpenNext environment variable guide ↗
You can convert an existing Next.js application to run on Cloudflare
Install
@opennextjs/cloudflare↗Terminal window npmi@opennextjs/cloudflare@latestTerminal window yarnadd@opennextjs/cloudflare@latestTerminal window pnpmadd@opennextjs/cloudflare@latestInstall
wrangler CLI↗ as a devDependencyTerminal window npmi-Dwrangler@latestTerminal window yarnadd-Dwrangler@latestTerminal window pnpmadd-Dwrangler@latestAdd a Wrangler configuration file
In your project root, create aWrangler configuration file with the following content:
{"$schema":"./node_modules/wrangler/config-schema.json","main":".open-next/worker.js","name":"my-app","compatibility_date":"2025-12-18","compatibility_flags":["nodejs_compat"],"assets":{"directory":".open-next/assets","binding":"ASSETS"}}main=".open-next/worker.js"name="my-app"compatibility_date="2025-12-18"compatibility_flags=["nodejs_compat"][assets]directory=".open-next/assets"binding="ASSETS"As shown above, you must enable the
nodejs_compatcompatibility flagand set yourcompatibility date to2024-09-23or later for your Next.js app to work with @opennextjs/cloudflare.Add a configuration file for OpenNext
In your project root, create an OpenNext configuration file named
open-next.config.tswith the following content:TypeScript import{defineCloudflareConfig} from"@opennextjs/cloudflare";exportdefaultdefineCloudflareConfig();open-next.config.tsis where you can configure the caching, see theadapter documentation ↗ for more informationUpdate
package.jsonYou can add the following scripts to your
package.json:"preview":"opennextjs-cloudflare build && opennextjs-cloudflare preview","deploy":"opennextjs-cloudflare build && opennextjs-cloudflare deploy","cf-typegen":"wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"Usage
preview: Builds your app and serves it locally, allowing you toquickly preview your app running locally in the Workers runtime, via asingle command.deploy: Builds your app, and then deploys it toCloudflarecf-typegen: Generates acloudflare-env.d.tsfile at theroot of your project containing the types for the env.
Develop locally.
After creating your project, run the following command in your project directory to start a local development server.The command uses the Next.js development server. It offers the best developer experience by quickly reloading your app after your source code is updated.
Terminal window npmrundevTerminal window yarnrundevTerminal window pnpmrundevTest your site with the Cloudflare adapter.
The command used in the previous step uses the Next.js development server to offer a great developer experience.However your application will run on Cloudflare Workers so you want to run your integration tests and verify that your application works correctly in this environment.
Terminal window npmrunpreviewTerminal window yarnrunpreviewTerminal window pnpmrunpreviewDeploy your project.
You can deploy your project to a
*.workers.devsubdomain or acustom domain from your local machine or any CI/CD system (includingWorkers Builds). Use the following command to build and deploy. If you're using a CI service, be sure to update your "deploy command" accordingly.Terminal window npmrundeployTerminal window yarnrundeployTerminal window pnpmrundeployWorkers Builds requires you to configure environment variables in the"Build Variables and secrets" section.
This ensures the Next build has the necessary access to both public
NEXT_PUBLC...variables andnon-NEXT_PUBLIC_...↗, which are essential for tasks like inlining and building SSG pages.Learn more in theOpenNext environment variable guide ↗
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark