import { Callout } from "nextra/components";
To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run:
npm create cloudflare@latest -- my-next-app --framework=next --platform=workersThe easiest way to convert an existing Next.js app is to use the migrate command:
npx @opennextjs/cloudflare migrateThis command automates all the setup steps below. If R2 is enabled on your account, it also creates an R2 bucket for caching. See the CLI documentation for more details.
Alternatively, you can follow the manual steps below.
First, install @opennextjs/cloudflare:
npm install @opennextjs/cloudflare@latestInstall the Wrangler CLI as a devDependency:
npm install --save-dev wrangler@latestA wrangler configuration file is needed for your application to be previewed and deployed, it is also where you configure your Worker and define what resources it can access via bindings.
You can create one yourself in the root directory of your Next.js app with the name wrangler.jsonc and the following content:
Add a open-next.config.ts file to the root directory of your Next.js app:
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
});Then, add a .dev.vars file to the root directory of your Next.js app:
NEXTJS_ENV=development
The NEXTJS_ENV variable defines the environment to use when loading Next.js .env files. It defaults to "production" when not defined.
Add the following to the scripts field of your package.json file:
"build": "next build",
"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
"upload": "opennextjs-cloudflare build && opennextjs-cloudflare upload",
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts",- The
buildscript must invoke the Next.js build command, it will be invoked byopennextjs-cloudflare build. npm run 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.npm run deploy: Builds your app, and then immediately deploys it to Cloudflare.npm run upload: Builds your app, and then uploads a new version of it to Cloudflare.cf-typegen: Generates acloudflare-env.d.tsfile at the root of your project containing the types for theenv.
Add a public/_headers file, with at least the following headers:
/_next/static/*
Cache-Control: public,max-age=31536000,immutableSee the Static Assets Caching docs for more information.
See the Caching docs for information on enabling Next.js caching in your OpenNext project.
Before deploying your app, remove the export const runtime = "edge"; line from any of your source files.
The edge runtime is not supported yet with @opennextjs/cloudflare.
You should add .open-next to your .gitignore file to prevent the build output from being committed to your repository.
If your Next.js app currently uses @cloudflare/next-on-pages, you'll want to remove it, and make a few changes.
Uninstalling the @cloudflare/next-on-pages package as well as the eslint-plugin-next-on-pages package if present.
Remove any reference of these packages from your source and configuration files. This includes:
setupDevPlatform()calls in your Next.js config filegetRequestContextimports from@cloudflare/next-on-pagesfrom your source files (those can be replaced withgetCloudflareContextcalls from@opennextjs/cloudflare)- next-on-pages eslint rules set in your Eslint config file
You can continue to run next dev when developing locally.
Modify your Next.js configuration file to import and call the initOpenNextCloudflareForDev utility
from the @opennextjs/cloudflare package. This makes sure that the Next.js dev server can optimally integrate with the open-next cloudflare adapter and it is necessary for using bindings during local development.
This is an example of a Next.js configuration file calling the utility:
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
/* config options here */
};
export default nextConfig;
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
initOpenNextCloudflareForDev();After having added the initOpenNextCloudflareForDev() call in your Next.js configuration file, you will be able, during local development, to access in any of your server code, local versions of Cloudflare bindings as indicated in the bindings documentation.
In step 3, we also added the npm run preview, which allows you to quickly preview your app running locally in the Workers runtime,
rather than in Node.js. This allows you to test changes in the same runtime as your app will run in when deployed to Cloudflare.
Either deploy via the command line:
npm run deployOr connect a Github or Gitlab repository, and Cloudflare will automatically build and deploy each pull request you merge to your production branch.
{ "$schema": "node_modules/wrangler/config-schema.json", "main": ".open-next/worker.js", "name": "my-app", "compatibility_date": "2024-12-30", "compatibility_flags": [ // Enable Node.js API // see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag "nodejs_compat", // Allow to fetch URLs in your app // see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-public "global_fetch_strictly_public", ], "assets": { "directory": ".open-next/assets", "binding": "ASSETS", }, "services": [ { "binding": "WORKER_SELF_REFERENCE", // The service should match the "name" of your worker "service": "my-app", }, ], "r2_buckets": [ // Create a R2 binding with the binding name "NEXT_INC_CACHE_R2_BUCKET" // { // "binding": "NEXT_INC_CACHE_R2_BUCKET", // "bucket_name": "<BUCKET_NAME>", // }, ], "images": { // Enable image optimization // see https://opennext.js.org/cloudflare/howtos/image "binding": "IMAGES", }, }