You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A [`wrangler.json`](https://developers.cloudflare.com/workers/wrangler/configuration/) file is needed for your
48
+
application to be previewed and deployed, it is also where you configure your Worker and define what resources it can access via [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings).
49
+
50
+
You can create one yourself with the following content:
51
+
```jsonc
52
+
{
53
+
"main":".open-next/worker.js",
54
+
"name":"my-app",
55
+
"compatibility_date":"2024-12-30",
56
+
"compatibility_flags": ["nodejs_compat"],
57
+
"assets": {
58
+
"directory":".open-next/assets",
59
+
"binding":"ASSETS",
60
+
},
61
+
"kv_namespaces": [
62
+
// Create a KV binding with the binding name "NEXT_CACHE_WORKERS_KV"
63
+
// to enable the KV based caching:
64
+
// {
65
+
// "binding": "NEXT_CACHE_WORKERS_KV",
66
+
// "id": "<BINDING_ID>"
67
+
// }
68
+
],
69
+
}
52
70
```
53
71
54
72
<Callout>
55
-
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.
73
+
As shown above:
74
+
- 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
75
+
- The `main` and `assets` values should also not be changed unless you modify the build output result in some way
76
+
- You can add a binding named `NEXT_CACHE_WORKERS_KV` to make use of Next.js' caching as described in the [Caching docs](/cloudflare/caching)
56
77
</Callout>
57
78
58
-
`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).
79
+
##### 4. Add an `open-next.config.ts` file
59
80
60
-
##### 3. Add a `open-next.config.ts` file
81
+
<Callouttype='info'>
82
+
This step is optional since `@opennextjs/cloudflare` creates this file for you during the build process (if not already present).
83
+
</Callout>
61
84
62
-
Then, add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app:
85
+
Add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app:
You can either install the `@opennextjs/aws` NPM package to get the types or `open-next.config.ts` to the [`exclude`](https://www.typescriptlang.org/tsconfig/#exclude) configuration key of your `tsconfig.json`.
116
+
<Callout>
117
+
To use the `OpenNextConfig` type as illustrated above (which is not necessary), you need to install the `@opennextjs/aws` NPM package as a dev dependency.
118
+
</Callout>
94
119
95
-
##### 4. Add a `.dev.vars` file
120
+
##### 5. Add a `.dev.vars` file
96
121
97
122
Then, add a [`.dev.vars`](https://developers.cloudflare.com/workers/testing/local-development/#local-only-environment-variables) file to the root directory of your Next.js app:
98
123
@@ -102,7 +127,7 @@ NEXTJS_ENV=development
102
127
103
128
The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `.env` files. It defaults to "production" when not defined.
104
129
105
-
##### 5. Update `package.json`
130
+
##### 6. Update the `package.json` file
106
131
107
132
Add the following to the scripts field of your `package.json` file:
108
133
@@ -118,70 +143,42 @@ Add the following to the scripts field of your `package.json` file:
118
143
-`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.
119
144
-`npm run deploy`: Builds your app, and then deploys it to Cloudflare
120
145
121
-
###6. Add caching with Workers KV
146
+
##### 7. Add caching with Workers KV
122
147
123
148
See the [Caching docs](/cloudflare/caching) for information on enabling Next.js caching in your OpenNext project.
- "preview": "npm run pages:build && wrangler pages dev",
148
-
- "deploy": "npm run pages:build && wrangler pages deploy"
149
-
150
-
"devDependencies": {
151
-
- "@cloudflare/next-on-pages": "*",
152
-
```
153
-
154
-
(remember to also remove [eslint-plugin-next-on-pages](https://www.npmjs.com/package/eslint-plugin-next-on-pages) from your `.eslintrc.js` file)
155
-
156
-
You no longer need to call `setupDevPlatform()` in your `next.config.mjs` file:
157
-
158
-
```diff title="next.config.mjs"
159
-
- import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev';
160
-
161
-
/** @type {import('next').NextConfig} */
162
-
const nextConfig = {};
163
-
164
-
- if (process.env.NODE_ENV === 'development') {
165
-
- await setupDevPlatform();
166
-
- }
167
-
```
162
+
If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes.
168
163
169
-
And you'll want to replace any uses of `getRequestContext` from `@cloudflare/next-on-pages` with `getCloudflareContext` from `@opennextjs/cloudflare`:
164
+
Uninstalling the [`@cloudflare/next-on-pages`](https://www.npmjs.com/package/@cloudflare/next-on-pages) package as well as the [`eslint-plugin-next-on-pages`](https://www.npmjs.com/package/eslint-plugin-next-on-pages) package if present.
170
165
171
-
```diff
172
-
- import { getRequestContext } from "@cloudflare/next-on-pages";
173
-
+ import { getCloudflareContext } from "@opennextjs/cloudflare";
174
-
```
166
+
Remove any reference of these packages from your source and configuration files.
167
+
This includes:
168
+
-`setupDevPlatform()` calls in your Next.js config file
169
+
-`getRequestContext` imports from `@cloudflare/next-on-pages` from your source files
170
+
(those can be replaced with `getCloudflareContext` calls from `@opennextjs/cloudflare`)
171
+
- next-on-pages eslint rules set in your Eslint config file
175
172
176
-
##### 8. Develop locally
173
+
##### 11. Develop locally
177
174
178
175
You can continue to run `next dev` when developing locally.
179
176
180
177
During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings).
181
178
182
179
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.
0 commit comments