Next.js
In this guide, you will create a new Next.js ↗ application and deploy to Cloudflare Workers (with the new Beta Workers Assets) using the @opennextjs/cloudflare
↗ package.
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=workers
pnpm create cloudflare@latest my-next-app --framework=next --platform=workers
yarn create cloudflare my-next-app --framework=next --platform=workers
For setup, select the following options:
- For What would you like to start with?, choose
Framework Starter
. - For Which development framework do you want to use?, choose
Next.js
. - Complete the framework's own CLI wizard.
- For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
First, install @opennextjs/cloudflare ↗:
npm install --save-dev @opennextjs/cloudflare
Then, add a Wrangler configuration file to the root directory of your Next.js app:
{ "main": ".open-next/worker.js", "name": "my-app", "compatibility_date": "2024-09-23", "compatibility_flags": [ "nodejs_compat" ], "assets": { "directory": ".open-next/assets", "binding": "ASSETS" }}
main = ".open-next/worker.js"name = "my-app"compatibility_date = "2024-09-23"compatibility_flags = ["nodejs_compat"]assets = { directory = ".open-next/assets", binding = "ASSETS" }
You configure your Worker and define what resources it can access via bindings in the Wrangler configuration file.
Add the following to the scripts field of your package.json
file:
"preview": "opennextjs-cloudflare && wrangler dev","deploy": "opennextjs-cloudflare && wrangler deploy","cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
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 Cloudflarecf-typegen
: Generates acloudflare-env.d.ts
file at the root of your project containing the types for the env.
Caching is actively being worked on. It is fully functional for development and we are working on an optimized implementation suitable for production.
For more details check the relevant official Open Next documentation ↗.
You can continue to run next dev
when developing locally.
In step 3, we also added the npm run preview
script, 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 that your app runs in, when deployed to Cloudflare:
npm run preview
This command will build your OpenNext application, also creating, if not already present, an open-next.configs.ts
file for you.
This is necessary if you want to deploy your application with a GibHub/GitLab integration as presented in the next step.
Either deploy via the command line:
npm run deploy
Or connect a GitHub or GitLab repository, and Cloudflare will automatically build and deploy each pull request you merge to your production branch.
You can serve static assets your Next.js application by placing them in the ./public/
directory. This can be useful for resource files such as images, stylesheets, fonts, and manifests.
By default, Cloudflare first tries to match a request path against a static asset path, which is based on the file structure of the uploaded asset directory. This is either the directory specified by assets.directory
in your Wrangler config or, in the case of the Cloudflare Vite plugin, the output directory of the client build. Failing that, we invoke a Worker if one is present. If there is no Worker, or the Worker then uses the asset binding, Cloudflare will fallback to the behaviour set by not_found_handling
.
Refer to the routing documentation for more information about how routing works with static assets, and how to customize this behavior.
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