|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: Next.js |
| 4 | +head: [] |
| 5 | +description: Create an Next.js application and deploy it to Cloudflare Workers with Workers Assets. |
| 6 | +--- |
| 7 | + |
| 8 | +import { |
| 9 | + Badge, |
| 10 | + Description, |
| 11 | + InlineBadge, |
| 12 | + Render, |
| 13 | + PackageManagers, |
| 14 | +} from "~/components"; |
| 15 | + |
| 16 | +#### New apps |
| 17 | + |
| 18 | +To create a new Next.js app, pre-configured to run on Cloudflare using [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), run: |
| 19 | + |
| 20 | +``` |
| 21 | +npm create cloudflare@latest --no-auto-update --experimental --framework next |
| 22 | +``` |
| 23 | + |
| 24 | +#### Existing Next.js apps |
| 25 | + |
| 26 | +::: note |
| 27 | +Minimum required Wrangler version: 3.78.10. Check your version by running `wrangler version`. To update Wrangler, refer to [Install/Update Wrangler](/workers/wrangler/install-and-update/). |
| 28 | +::: |
| 29 | + |
| 30 | +##### 1. Install @opennextjs/cloudflare |
| 31 | + |
| 32 | +First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare): |
| 33 | + |
| 34 | +```sh |
| 35 | +npm install --save-dev @opennextjs/cloudflare |
| 36 | +``` |
| 37 | + |
| 38 | +##### 2. Add a `wrangler.toml` file |
| 39 | + |
| 40 | +Then, add a [`wrangler.toml`](/workers/wrangler/configuration/) file to the root directory of your Next.js app: |
| 41 | + |
| 42 | +```toml |
| 43 | +main = ".worker-next/index.mjs" |
| 44 | +name = "my-app" |
| 45 | +compatibility_date = "2024-09-23" |
| 46 | +compatibility_flags = ["nodejs_compat"] |
| 47 | +experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" } |
| 48 | +``` |
| 49 | + |
| 50 | +:::note |
| 51 | +As shown above, you must enable the [`nodejs_compat` compatibility flag](/workers/runtime-apis/nodejs/) *and* set your [compatibility date](/workers/configuration/compatibility-dates/) to `2024-09-23` or later for your Next.js app to work with @opennextjs/cloudflare. |
| 52 | +::: |
| 53 | + |
| 54 | +`wrangler.toml` is where you configure your Worker and define what resources it can access via [bindings](/workers/runtime-apis/bindings/). |
| 55 | + |
| 56 | +##### 3. Update `package.json` |
| 57 | + |
| 58 | +Add the following to the scripts field of your `package.json` file: |
| 59 | + |
| 60 | +```json |
| 61 | +"build:worker": "cloudflare", |
| 62 | +"dev:worker": "wrangler dev --port 8771", |
| 63 | +"preview:worker": "npm run build:worker && npm run dev:worker", |
| 64 | +"deploy:worker": "npm run build:worker && wrangler deploy" |
| 65 | +``` |
| 66 | + |
| 67 | +- `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](/workers/wrangler/) and deploy to Cloudflare. |
| 68 | +- `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. |
| 69 | +- `npm run preview:worker`: Runs `build:worker` and then `preview:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command. |
| 70 | +- `npm run deploy`: Builds your app, and then deploys it to Cloudflare |
| 71 | + |
| 72 | +### 4. Add caching with Workers KV |
| 73 | + |
| 74 | +`opennextjs/cloudflare` uses [Workers KV](/kv/) as the cache for your Next.js app. Workers KV is [fast](https://blog.cloudflare.com/faster-workers-kv) and uses Cloudflare's [Tiered Cache](/cache/how-to/tiered-cache/) to increase cache hit rates. When you write cached data to Workers KV, you write to storage that can be read by any Cloudflare location. This means your app can fetch data, cache it in KV, and then be read by subsequent requests from this cache anywhere in the world. |
| 75 | + |
| 76 | +To enable caching, you must: |
| 77 | + |
| 78 | +##### Create a KV namespace |
| 79 | + |
| 80 | +``` |
| 81 | +npx wrangler@latest kv namespace create NEXT_CACHE_WORKERS_KV |
| 82 | +``` |
| 83 | + |
| 84 | +##### Add the KV namespace to your Worker |
| 85 | + |
| 86 | +``` |
| 87 | +[[kv_namespaces]] |
| 88 | +binding = "NEXT_CACHE_WORKERS_KV" |
| 89 | +id = "<YOUR_NAMESPACE_ID>" |
| 90 | +``` |
| 91 | + |
| 92 | +#### Set the name of the binding to `NEXT_CACHE_WORKERS_KV` |
| 93 | + |
| 94 | +As shown above, the name of the binding that you configure for the KV namespace must be set to `NEXT_CACHE_WORKERS_KV`. |
| 95 | + |
| 96 | +##### 5. Develop locally |
| 97 | + |
| 98 | +You can continue to run `next dev` when developing locally. |
| 99 | + |
| 100 | +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 that your app runs in, when deployed to Cloudflare. |
| 101 | + |
| 102 | +##### 6. Deploy to Cloudflare Workers |
| 103 | + |
| 104 | +Either deploy via the command line: |
| 105 | + |
| 106 | +```sh |
| 107 | +npm run deploy |
| 108 | +``` |
| 109 | + |
| 110 | +Or [connect a GitHub or GitLab repository](/workers/ci-cd/), and Cloudflare will automatically build and deploy each pull request you merge to your production branch. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Static assets |
| 115 | + |
| 116 | +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. |
| 117 | + |
| 118 | +<Render file="workers-assets-routing-summary" /> |
0 commit comments