Next.js
Start from CLI - scaffold a Next.js project on Workers.
npm create cloudflare@latest my-next-app -- --framework=next --platform=workers
pnpm create cloudflare@latest my-next-app --framework=next --platform=workers
yarn create cloudflare my-next-app --framework=next --platform=workers
This is a simple getting started guide. For detailed documentation on how the to use the Cloudflare OpenNext adapter, visit the OpenNext website ↗.
Next.js ↗ is a React ↗ 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 adaptor.
-
Create a new project with the create-cloudflare CLI (C3).
Terminal window npm create cloudflare@latest my-next-app -- --framework=next --platform=workersTerminal window pnpm create cloudflare@latest my-next-app --framework=next --platform=workersTerminal window yarn create cloudflare my-next-app --framework=next --platform=workersWhat's happening behind the scenes?
When you run this command, C3 creates a new project directory, initiates Next.js's official setup tool ↗, and configures the project for Cloudflare. It then offers the option to instantly 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 npm run devTerminal window pnpm run devTerminal window yarn run dev -
Test and preview your site with the Cloudflare adapter.
Terminal window npm run previewTerminal window pnpm run previewTerminal window yarn run previewWhat'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 on Cloudflare Workers, which uses the
workerd
runtime. Therefore when running integration tests and previewing your application, you should use the preview command, which is more accurate to production, as it executes your application in theworkerd
runtime usingwrangler dev
. -
Deploy your project.
You can deploy your project to a
*.workers.dev
subdomain or a custom domain from your local machine or any CI/CD system (including Workers 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 npm run deployTerminal window pnpm run deployTerminal window yarn run deploy
You can convert an existing Next.js application to run on Cloudflare
-
Install
@opennextjs/cloudflare
↗Terminal window npm install @opennextjs/cloudflare@latestTerminal window pnpm install @opennextjs/cloudflare@latestTerminal window yarn install @opennextjs/cloudflare@latest -
Add a Wrangler configuration file
In your project root, create a Wrangler configuration file with the following content:
{"main": ".open-next/worker.js","name": "my-app","compatibility_date": "2025-03-25","compatibility_flags": ["nodejs_compat"],"assets": {"directory": ".open-next/assets","binding": "ASSETS"}}main = ".open-next/worker.js"name = "my-app"compatibility_date = "2025-03-25"compatibility_flags = ["nodejs_compat"][assets]directory = ".open-next/assets"binding = "ASSETS" -
Add a configuration file for OpenNext
In your project root, create an OpenNext configuration file named
open-next.config.ts
with the following content:import { defineCloudflareConfig } from "@opennextjs/cloudflare";export default defineCloudflareConfig(); -
Update
package.json
You 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 to quickly preview your app running locally in the Workers runtime, via a single command. -deploy
: Builds your app, and then deploys it to Cloudflare -cf-typegen
: Generates acloudflare-env.d.ts
file at the root 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 npm run devTerminal window pnpm run devTerminal window yarn run dev -
Test 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 workers correctly in this environment.
Terminal window npm run previewTerminal window pnpm run previewTerminal window yarn run preview -
Deploy your project.
You can deploy your project to a
*.workers.dev
subdomain or a custom domain from your local machine or any CI/CD system (including Workers 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 npm run deployTerminal window pnpm run deployTerminal window yarn run deploy
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- 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