Skip to content

Commit 2beac6f

Browse files
update and improve the Cloudflare adapter getting started guide (#45)
1 parent 1ed0733 commit 2beac6f

File tree

2 files changed

+62
-65
lines changed

2 files changed

+62
-65
lines changed

pages/cloudflare/get-started.mdx

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,77 @@ Use the template from `flarelabs-net/workers-next` as indicated above to use 0.3
2323
First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare):
2424

2525
```sh
26-
npm install --save-dev @opennextjs/cloudflare
26+
npm install --save-dev @opennextjs/cloudflare@latest
2727
```
2828

29-
##### 2. Install Wrangler, and add a `wrangler.toml` file
29+
##### 2. Install Wrangler
3030

3131
Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency:
3232

3333
```npm
34-
npm install -D wrangler@latest
34+
npm install --save-dev wrangler@latest
3535
```
3636

3737
<Callout>
3838
You must use Wrangler version `3.99.0` or later to deploy Next.js apps using `@opennextjs/cloudflare`.
3939
</Callout>
4040

41-
Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app:
41+
##### 3. Create a `wrangler.json` file
4242

43-
```toml
44-
main = ".open-next/worker.js"
45-
name = "my-app"
46-
47-
compatibility_date = "2024-09-23"
48-
compatibility_flags = ["nodejs_compat"]
43+
<Callout type='info'>
44+
This step is optional since `@opennextjs/cloudflare` creates this file for you during the build process (if not already present).
45+
</Callout>
4946

50-
# The binding name must be "ASSETS" when the cache is enabled
51-
assets = { directory = ".open-next/assets", binding = "ASSETS" }
47+
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+
}
5270
```
5371

5472
<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)
5677
</Callout>
5778

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
5980

60-
##### 3. Add a `open-next.config.ts` file
81+
<Callout type='info'>
82+
This step is optional since `@opennextjs/cloudflare` creates this file for you during the build process (if not already present).
83+
</Callout>
6184

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:
6386

6487
```ts
65-
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
88+
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
6689
import cache from "@opennextjs/cloudflare/kvCache";
6790

6891
const config: OpenNextConfig = {
6992
default: {
7093
override: {
7194
wrapper: "cloudflare-node",
7295
converter: "edge",
73-
// Set `incrementalCache` to "dummy" to disable KV cache
96+
// set `incrementalCache` to "dummy" to disable KV cache
7497
incrementalCache: async () => cache,
7598
tagCache: "dummy",
7699
queue: "dummy",
@@ -90,9 +113,11 @@ const config: OpenNextConfig = {
90113
export default config;
91114
```
92115

93-
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>
94119

95-
##### 4. Add a `.dev.vars` file
120+
##### 5. Add a `.dev.vars` file
96121

97122
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:
98123

@@ -102,7 +127,7 @@ NEXTJS_ENV=development
102127

103128
The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `.env` files. It defaults to "production" when not defined.
104129

105-
##### 5. Update `package.json`
130+
##### 6. Update the `package.json` file
106131

107132
Add the following to the scripts field of your `package.json` file:
108133

@@ -118,70 +143,42 @@ Add the following to the scripts field of your `package.json` file:
118143
- `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.
119144
- `npm run deploy`: Builds your app, and then deploys it to Cloudflare
120145

121-
### 6. Add caching with Workers KV
146+
##### 7. Add caching with Workers KV
122147

123148
See the [Caching docs](/cloudflare/caching) for information on enabling Next.js caching in your OpenNext project.
124149

125-
### 7. Remove `@cloudflare/next-on-pages` (if necessary)
126-
127-
If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes.
150+
##### 8. Remove any `export const runtime = "edge";` if present
128151

129-
#### Remove `export const runtime = "edge";`
152+
Before deploying your app, remove the `export const runtime = "edge";` line from any of your source files.
130153

131-
Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file.
132154
The edge runtime is not supported yet with `@opennextjs/cloudflare`.
133155

134-
#### Add `.open-next` to `.gitignore`
156+
##### 9. Add `.open-next` to `.gitignore`
135157

136158
You should add `.open-next` to your `.gitignore` file to prevent the build output from being committed to your repository.
137159

138-
#### Uninstall `@cloudflare/next-on-pages`
139-
140-
You should uninstall `@cloudflare/next-on-pages` and remove any references to it.
141-
142-
In `package.json`:
160+
##### 10. Remove `@cloudflare/next-on-pages` (if necessary)
143161

144-
```diff
145-
"scripts": {
146-
- "pages:build": "npx @cloudflare/next-on-pages",
147-
- "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.
168163

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.
170165

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
175172

176-
##### 8. Develop locally
173+
##### 11. Develop locally
177174

178175
You can continue to run `next dev` when developing locally.
179176

180177
During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings).
181178

182179
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.
183180

184-
##### 9. Deploy to Cloudflare Workers
181+
##### 12. Deploy to Cloudflare Workers
185182

186183
Either deploy via the command line:
187184

pages/cloudflare/migrate-from-0.2.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ assets = { directory = ".open-next/assets", binding = "ASSETS" }
2929
Add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app:
3030

3131
```ts
32-
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
32+
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
3333
import cache from "@opennextjs/cloudflare/kvCache";
3434

3535
const config: OpenNextConfig = {

0 commit comments

Comments
 (0)