|
| 1 | +import { SITE } from '../../../config'; |
| 2 | +import { Callout } from 'nextra/components'; |
| 3 | + |
| 4 | +### Get Started |
| 5 | + |
| 6 | +#### New apps |
| 7 | + |
| 8 | +To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run: |
| 9 | + |
| 10 | +``` |
| 11 | +npm create cloudflare@latest -- my-next-app --framework=next --experimental |
| 12 | +``` |
| 13 | + |
| 14 | +#### Existing Next.js apps |
| 15 | + |
| 16 | +##### 1. Install @opennextjs/cloudflare |
| 17 | + |
| 18 | +First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare): |
| 19 | + |
| 20 | +```sh |
| 21 | +npm install --save-dev @opennextjs/cloudflare |
| 22 | +``` |
| 23 | + |
| 24 | +##### 2. Install Wrangler, and add a `wrangler.toml` file |
| 25 | + |
| 26 | +Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency: |
| 27 | + |
| 28 | +```npm |
| 29 | +npm install -D wrangler@latest |
| 30 | +``` |
| 31 | + |
| 32 | +<Callout> |
| 33 | +You must use Wrangler version `3.78.10` or later to deploy Next.js apps using `@opennextjs/cloudflare`. |
| 34 | +</Callout> |
| 35 | + |
| 36 | +Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app: |
| 37 | + |
| 38 | +```toml |
| 39 | +main = ".worker-next/index.mjs" |
| 40 | +name = "my-app" |
| 41 | +compatibility_date = "2024-09-23" |
| 42 | +compatibility_flags = ["nodejs_compat"] |
| 43 | +assets = { directory = ".worker-next/assets", binding = "ASSETS" } |
| 44 | +``` |
| 45 | + |
| 46 | +<Callout> |
| 47 | +As shown above, you must enable the [`nodejs_compat` compatibility flag](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) *and* set your [compatibility date](https://developers.cloudflare.com/workers/configuration/compatibility-dates/) to `2024-09-23` or later, in order for your Next.js app to work with @opennextjs/cloudflare. |
| 48 | +</Callout> |
| 49 | + |
| 50 | +`wrangler.toml` is where you configure your Worker and define what resources it can access via [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings). |
| 51 | + |
| 52 | +##### 3. Update `package.json` |
| 53 | + |
| 54 | +Add the following to the scripts field of your `package.json` file: |
| 55 | + |
| 56 | +```json |
| 57 | +"build:worker": "cloudflare", |
| 58 | +"dev:worker": "wrangler dev --port 8771", |
| 59 | +"preview:worker": "npm run build:worker && npm run dev:worker", |
| 60 | +"deploy:worker": "npm run build:worker && wrangler deploy" |
| 61 | +``` |
| 62 | + |
| 63 | +- `npm run build:worker`: Runs the [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) adapter. This first builds your app by running `next build` behind the scenes, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare. |
| 64 | +- `npm run dev:worker`: Takes the output generated by `build:worker` and runs it locally in [workerd](https://github.com/cloudflare/workerd), the open-source Workers Runtime, allowing you to run the app locally in the same environment that it will run in production. If you instead run `next dev`, your app will run in Node.js, which is a different JavaScript runtime from the Workers runtime, with differences in behavior and APIs. |
| 65 | +- `npm run preview:worker`: Runs `build:worker` and then `dev:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command. |
| 66 | +- `npm run deploy`: Builds your app, and then deploys it to Cloudflare |
| 67 | + |
| 68 | +### 4. Add caching with Workers KV |
| 69 | + |
| 70 | +See the [Caching docs](/cloudflare/0.2/caching) for information on enabling Next.js caching in your OpenNext project. |
| 71 | + |
| 72 | +### 5. Remove `@cloudflare/next-on-pages` (if necessary) |
| 73 | + |
| 74 | +If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes. |
| 75 | + |
| 76 | +#### Remove `export const runtime = "edge";` |
| 77 | + |
| 78 | +Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file. This line is not needed when using `@opennextjs/cloudflare`. |
| 79 | + |
| 80 | +#### Add `.worker-next` to `.gitignore` |
| 81 | + |
| 82 | +You should add `.worker-next` to your `.gitignore` file to prevent the build output from being committed to your repository. |
| 83 | + |
| 84 | +#### Uninstall `@cloudflare/next-on-pages` |
| 85 | + |
| 86 | +You should uninstall `@cloudflare/next-on-pages` and remove any references to it. |
| 87 | + |
| 88 | +In `package.json`: |
| 89 | + |
| 90 | +```diff |
| 91 | +"scripts": { |
| 92 | +- "pages:build": "npx @cloudflare/next-on-pages", |
| 93 | +- "preview": "npm run pages:build && wrangler pages dev", |
| 94 | +- "deploy": "npm run pages:build && wrangler pages deploy" |
| 95 | + |
| 96 | +"devDependencies": { |
| 97 | +- "@cloudflare/next-on-pages": "*", |
| 98 | +``` |
| 99 | + |
| 100 | +(remember to also remove [eslint-plugin-next-on-pages](https://www.npmjs.com/package/eslint-plugin-next-on-pages) from your `.eslintrc.js` file) |
| 101 | + |
| 102 | +You no longer need to call `setupDevPlatform()` in your `next.config.mjs` file: |
| 103 | + |
| 104 | +```diff title="next.config.mjs" |
| 105 | +- import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev'; |
| 106 | + |
| 107 | +/** @type {import('next').NextConfig} */ |
| 108 | +const nextConfig = {}; |
| 109 | + |
| 110 | +- if (process.env.NODE_ENV === 'development') { |
| 111 | +- await setupDevPlatform(); |
| 112 | +- } |
| 113 | +``` |
| 114 | + |
| 115 | +And you'll want to replace any uses of `getRequestContext` from `@cloudflare/next-on-pages` with `getCloudflareContext` from `@opennextjs/cloudflare`: |
| 116 | + |
| 117 | +```diff |
| 118 | +- import { getRequestContext } from "@cloudflare/next-on-pages"; |
| 119 | ++ import { getCloudflareContext } from "@opennextjs/cloudflare"; |
| 120 | +``` |
| 121 | + |
| 122 | +##### 6. Develop locally |
| 123 | + |
| 124 | +You can continue to run `next dev` when developing locally. |
| 125 | + |
| 126 | +During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings). |
| 127 | + |
| 128 | +In step 3, we also added the `npm run preview:worker`, 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. |
| 129 | + |
| 130 | +##### 7. Deploy to Cloudflare Workers |
| 131 | + |
| 132 | +Either deploy via the command line: |
| 133 | + |
| 134 | +```sh |
| 135 | +npm run deploy:worker |
| 136 | +``` |
| 137 | + |
| 138 | +Or [connect a Github or Gitlab repository](https://developers.cloudflare.com/workers/ci-cd/), and Cloudflare will automatically build and deploy each pull request you merge to your production branch. |
0 commit comments