Skip to content

Commit cdd2088

Browse files
Update the documentation for Next on Workers (cloudflare#20908)
Co-authored-by: Dario Piotrowicz <[email protected]>
1 parent c27efb5 commit cdd2088

File tree

1 file changed

+19
-40
lines changed
  • src/content/docs/workers/frameworks/framework-guides

1 file changed

+19
-40
lines changed

src/content/docs/workers/frameworks/framework-guides/nextjs.mdx

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import {
1515
WranglerConfig
1616
} from "~/components";
1717

18+
In this guide, you will create a new [Next.js](https://nextjs.org/) application and deploy to Cloudflare Workers (with the new [<InlineBadge preset="beta" /> Workers Assets](/workers/static-assets/)) using the [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare) package.
19+
20+
:::note
21+
This is a simple getting started guide for a more detailed documentation on how the to use the Cloudflare Open Next adapter visit the [Open Next website](https://opennext.js.org/cloudflare).
22+
:::
1823
## New apps
1924

2025
To create a new Next.js app, pre-configured to run on Cloudflare using [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), run:
@@ -36,7 +41,7 @@ To create a new Next.js app, pre-configured to run on Cloudflare using [`@openne
3641

3742
## Existing Next.js apps
3843

39-
:::note[Minimum required Wrangler version: 3.78.10.]
44+
:::note[Minimum required Wrangler version: 3.99.0.]
4045

4146
Check your version by running `wrangler --version`. To update Wrangler, refer to [Install/Update Wrangler](/workers/wrangler/install-and-update/).
4247

@@ -77,58 +82,32 @@ You configure your Worker and define what resources it can access via [bindings]
7782
Add the following to the scripts field of your `package.json` file:
7883

7984
```json
80-
"build:worker": "opennextjs-cloudflare",
81-
"dev:worker": "wrangler dev --port 8771",
82-
"preview:worker": "npm run build:worker && npm run dev:worker",
83-
"deploy:worker": "npm run build:worker && wrangler deploy"
84-
```
85-
86-
- `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.
87-
- `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.
88-
- `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.
89-
- `npm run deploy:worker`: Builds your app, and then deploys it to Cloudflare
90-
91-
### 4. Add caching with Workers KV
92-
93-
`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.
94-
95-
To enable caching, you must:
96-
97-
#### Create a KV namespace
98-
99-
```sh
100-
npx wrangler@latest kv namespace create NEXT_CACHE_WORKERS_KV
85+
"preview": "opennextjs-cloudflare && wrangler dev",
86+
"deploy": "opennextjs-cloudflare && wrangler deploy",
87+
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
10188
```
10289

103-
#### Add the KV namespace to your Worker
90+
- `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.
91+
- `deploy`: Builds your app, and then deploys it to Cloudflare
92+
- `cf-typegen`: Generates a `cloudflare-env.d.ts` file at the root of your project containing the types for the env.
10493

105-
<WranglerConfig>
94+
### 4. Optionally add caching
10695

107-
```toml
108-
[[kv_namespaces]]
109-
binding = "NEXT_CACHE_WORKERS_KV"
110-
id = "<YOUR_NAMESPACE_ID>"
111-
```
96+
Caching is actively being worked on. It is fully functional for development and we are working on an optimized implementation suitable for production.
11297

113-
</WranglerConfig>
114-
115-
#### Set the name of the binding to `NEXT_CACHE_WORKERS_KV`
116-
117-
As shown above, the name of the binding that you configure for the KV namespace must be set to `NEXT_CACHE_WORKERS_KV`.
98+
For more details check the relevant [official Open Next documentation](https://opennext.js.org/cloudflare/caching).
11899

119100
### 5. Develop locally
120101

121102
You can continue to run `next dev` when developing locally.
122103

123104
### 6. Preview locally your application and create an OpenNext config file
124105

125-
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.
126-
This allows you to test changes in the same runtime that your app runs in, when deployed to Cloudflare.
127-
128-
To preview your application in such way run:
106+
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.
107+
This allows you to test changes in the same runtime that your app runs in, when deployed to Cloudflare:
129108

130109
```sh
131-
npm run preview:worker
110+
npm run preview
132111
```
133112

134113
This command will build your OpenNext application, also creating, if not already present, an `open-next.configs.ts` file for you.
@@ -139,7 +118,7 @@ This is necessary if you want to deploy your application with a GibHub/GitLab in
139118
Either deploy via the command line:
140119

141120
```sh
142-
npm run deploy:worker
121+
npm run deploy
143122
```
144123

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

0 commit comments

Comments
 (0)