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
Copy file name to clipboardExpand all lines: src/content/docs/workers/frameworks/framework-guides/nextjs.mdx
+19-40Lines changed: 19 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,11 @@ import {
15
15
WranglerConfig
16
16
} from"~/components";
17
17
18
+
In this guide, you will create a new [Next.js](https://nextjs.org/) application and deploy to Cloudflare Workers (with the new [<InlineBadgepreset="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
+
:::
18
23
## New apps
19
24
20
25
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
Check your version by running `wrangler --version`. To update Wrangler, refer to [Install/Update Wrangler](/workers/wrangler/install-and-update/).
42
47
@@ -77,58 +82,32 @@ You configure your Worker and define what resources it can access via [bindings]
77
82
Add the following to the scripts field of your `package.json` file:
78
83
79
84
```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.
-`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.
104
93
105
-
<WranglerConfig>
94
+
### 4. Optionally add caching
106
95
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.
112
97
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).
118
99
119
100
### 5. Develop locally
120
101
121
102
You can continue to run `next dev` when developing locally.
122
103
123
104
### 6. Preview locally your application and create an OpenNext config file
124
105
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:
129
108
130
109
```sh
131
-
npm run preview:worker
110
+
npm run preview
132
111
```
133
112
134
113
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
139
118
Either deploy via the command line:
140
119
141
120
```sh
142
-
npm run deploy:worker
121
+
npm run deploy
143
122
```
144
123
145
124
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