Skip to content

Commit d0a8a11

Browse files
authored
cloudflare: update docs for 0.3 (#41)
1 parent ad8b1ee commit d0a8a11

File tree

12 files changed

+517
-27
lines changed

12 files changed

+517
-27
lines changed

pages/cloudflare/0.2/_meta.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"index": "Overview",
3+
"get-started": "",
4+
"bindings": "",
5+
"caching": "",
6+
"examples": ""
7+
}

pages/cloudflare/0.2/bindings.mdx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { SITE } from '../../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
### Bindings
5+
6+
[Bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) allow your Worker to interact with resources on the Cloudflare Developer Platform. When you declare a binding on your Worker, you grant it a specific capability, such as being able to read and write files to an [R2](https://developers.cloudflare.com/r2/) bucket.
7+
8+
#### How to configure your Next.js app so it can access bindings
9+
10+
Install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare), and then add a `wrangler.toml` file in the root directory of your Next.js app, as described in [Get Started](/cloudflare/get-started).
11+
12+
#### How to access bindings in your Next.js app
13+
14+
You can access [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) from any route of your Next.js app via `getCloudflareContext`:
15+
16+
```js
17+
import { getCloudflareContext } from "@opennextjs/cloudflare";
18+
19+
export async function GET(request) {
20+
let responseText = "Hello World";
21+
22+
const myKv = (await getCloudflareContext()).env.MY_KV_NAMESPACE;
23+
await myKv.put("foo", "bar");
24+
const foo = await myKv.get("foo");
25+
26+
return new Response(foo);
27+
}
28+
```
29+
30+
#### How to add bindings to your Worker
31+
32+
Add bindings to your Worker by [adding them to your `wrangler.toml` configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/).
33+
34+
## TypeScript type declarations for bindings
35+
36+
To ensure that the `env` object from `(await getCloudflareContext()).env` above has accurate TypeScript types, run the following Wrangler command to [generate types that match your Worker's configuration](https://developers.cloudflare.com/workers/languages/typescript/#generate-types-that-match-your-workers-configuration-experimental):
37+
38+
```
39+
npx wrangler types --experimental-include-runtime
40+
```
41+
42+
This will generate a `d.ts` file and (by default) save it to `.wrangler/types/runtime.d.ts`. You will be prompted in the command's output to add that file to your `tsconfig.json`'s `compilerOptions.types` array.
43+
44+
If you would like to commit the file to git, you can provide a custom path. Here, for instance, the `runtime.d.ts` file will be saved to the root of your project:
45+
46+
```bash
47+
npx wrangler types --experimental-include-runtime="./runtime.d.ts"
48+
```
49+
50+
To ensure that your types are always up-to-date, make sure to run `wrangler types --experimental-include-runtime` after any changes to your config file.
51+
52+
## Other Cloudflare APIs (`cf`, `ctx`)
53+
54+
You can access context about the incoming request from the [`cf` object](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties), as well as lifecycle methods from the [`ctx` object](https://developers.cloudflare.com/workers/runtime-apis/context) from the return value of [`getCloudflareContext()`](https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/src/api/get-cloudflare-context.ts):
55+
56+
```js
57+
import { getCloudflareContext } from "@opennextjs/cloudflare";
58+
59+
60+
export async function GET(request) {
61+
const { env, cf, ctx } = await getCloudflareContext();
62+
63+
// ...
64+
}
65+
```

pages/cloudflare/0.2/caching.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { SITE } from '../../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
## Caching
5+
6+
`@opennextjs/cloudflare` supports [caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data) and [revalidating](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) data returned by subrequests you make in your app by calling [`fetch()`](https://developers.cloudflare.com/workers/runtime-apis/fetch/).
7+
8+
By default, all `fetch()` subrequests made in your Next.js app are cached. Refer to the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/caching#opting-out-1) for information about how to disable caching for an individual subrequest, or for an entire route.
9+
10+
[The cache persists across deployments](https://nextjs.org/docs/app/building-your-application/caching#data-cache). You are responsible for revalidating/purging this cache.
11+
12+
<Callout>
13+
Workers KV is eventually consistent, which means that it can take up to 60 seconds for updates to be reflected globally, when using the default TTL of 60 seconds.
14+
</Callout>
15+
16+
### How to enable caching
17+
18+
`@opennextjs/cloudflare` uses [Workers KV](https://developers.cloudflare.com/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](https://developers.cloudflare.com/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 subsequent requests anywhere around the world can read from this cache.
19+
20+
To enable caching, you must:
21+
22+
#### 1. Create a KV namespace
23+
24+
```
25+
npx wrangler@latest kv namespace create <YOUR_NAMESPACE_NAME>
26+
```
27+
28+
#### 2. Add the KV namespace to your Worker
29+
30+
The binding name used in your app's worker will be `NEXT_CACHE_WORKERS_KV` by default. This is configurable and can be changed by setting the `__OPENNEXT_KV_BINDING_NAME` build-time environment variable.
31+
32+
```
33+
[[kv_namespaces]]
34+
binding = "NEXT_CACHE_WORKERS_KV"
35+
id = "<YOUR_NAMESPACE_ID>"
36+
```

pages/cloudflare/0.2/examples.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { SITE } from '../../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
## Examples
5+
6+
To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run:
7+
8+
```
9+
npm create cloudflare@latest -- my-next-app --framework=next --experimental
10+
```
11+
12+
### Basic starter projects
13+
14+
Two basic example apps are included in the repository for `@opennextjs/cloudflare` package:
15+
16+
- [*`create-next-app`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/create-next-app) — a Next.js project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
17+
- [*`api`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/api) — a minimal Next.js project with a single API route
18+
19+
You can use these to understand how to configure your Next.js app to use `@opennextjs/cloudflare`, or refer to [Get Started](/cloudflare/get-started).
20+
21+
### Next.js Commerce Demo
22+
23+
The [Next.js Commerce demo app](https://github.com/vercel/commerce/tree/v1) works with `@opennextjs/cloudflare`. You can view a deployed version of it [here](https://vercel-commerce-on-workers.web-experiments.workers.dev/).
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { SITE } from '../../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
### Get Started
5+
6+
#### New apps
7+
8+
To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run:
9+
10+
```
11+
npm create cloudflare@latest -- my-next-app --framework=next --experimental
12+
```
13+
14+
#### Existing Next.js apps
15+
16+
##### 1. Install @opennextjs/cloudflare
17+
18+
First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare):
19+
20+
```sh
21+
npm install --save-dev @opennextjs/cloudflare
22+
```
23+
24+
##### 2. Install Wrangler, and add a `wrangler.toml` file
25+
26+
Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency:
27+
28+
```npm
29+
npm install -D wrangler@latest
30+
```
31+
32+
<Callout>
33+
You must use Wrangler version `3.78.10` or later to deploy Next.js apps using `@opennextjs/cloudflare`.
34+
</Callout>
35+
36+
Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app:
37+
38+
```toml
39+
main = ".worker-next/index.mjs"
40+
name = "my-app"
41+
compatibility_date = "2024-09-23"
42+
compatibility_flags = ["nodejs_compat"]
43+
assets = { directory = ".worker-next/assets", binding = "ASSETS" }
44+
```
45+
46+
<Callout>
47+
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.
48+
</Callout>
49+
50+
`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).
51+
52+
##### 3. Update `package.json`
53+
54+
Add the following to the scripts field of your `package.json` file:
55+
56+
```json
57+
"build:worker": "cloudflare",
58+
"dev:worker": "wrangler dev --port 8771",
59+
"preview:worker": "npm run build:worker && npm run dev:worker",
60+
"deploy:worker": "npm run build:worker && wrangler deploy"
61+
```
62+
63+
- `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](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare.
64+
- `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.
65+
- `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.
66+
- `npm run deploy`: Builds your app, and then deploys it to Cloudflare
67+
68+
### 4. Add caching with Workers KV
69+
70+
See the [Caching docs](/cloudflare/0.2/caching) for information on enabling Next.js caching in your OpenNext project.
71+
72+
### 5. Remove `@cloudflare/next-on-pages` (if necessary)
73+
74+
If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes.
75+
76+
#### Remove `export const runtime = "edge";`
77+
78+
Before deploying your app, remove the `export const runtime = "edge";` line from your `next.config.js` file. This line is not needed when using `@opennextjs/cloudflare`.
79+
80+
#### Add `.worker-next` to `.gitignore`
81+
82+
You should add `.worker-next` to your `.gitignore` file to prevent the build output from being committed to your repository.
83+
84+
#### Uninstall `@cloudflare/next-on-pages`
85+
86+
You should uninstall `@cloudflare/next-on-pages` and remove any references to it.
87+
88+
In `package.json`:
89+
90+
```diff
91+
"scripts": {
92+
- "pages:build": "npx @cloudflare/next-on-pages",
93+
- "preview": "npm run pages:build && wrangler pages dev",
94+
- "deploy": "npm run pages:build && wrangler pages deploy"
95+
96+
"devDependencies": {
97+
- "@cloudflare/next-on-pages": "*",
98+
```
99+
100+
(remember to also remove [eslint-plugin-next-on-pages](https://www.npmjs.com/package/eslint-plugin-next-on-pages) from your `.eslintrc.js` file)
101+
102+
You no longer need to call `setupDevPlatform()` in your `next.config.mjs` file:
103+
104+
```diff title="next.config.mjs"
105+
- import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev';
106+
107+
/** @type {import('next').NextConfig} */
108+
const nextConfig = {};
109+
110+
- if (process.env.NODE_ENV === 'development') {
111+
- await setupDevPlatform();
112+
- }
113+
```
114+
115+
And you'll want to replace any uses of `getRequestContext` from `@cloudflare/next-on-pages` with `getCloudflareContext` from `@opennextjs/cloudflare`:
116+
117+
```diff
118+
- import { getRequestContext } from "@cloudflare/next-on-pages";
119+
+ import { getCloudflareContext } from "@opennextjs/cloudflare";
120+
```
121+
122+
##### 6. Develop locally
123+
124+
You can continue to run `next dev` when developing locally.
125+
126+
During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings).
127+
128+
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.
129+
130+
##### 7. Deploy to Cloudflare Workers
131+
132+
Either deploy via the command line:
133+
134+
```sh
135+
npm run deploy:worker
136+
```
137+
138+
Or [connect a Github or Gitlab repository](https://developers.cloudflare.com/workers/ci-cd/), and Cloudflare will automatically build and deploy each pull request you merge to your production branch.

pages/cloudflare/0.2/index.mdx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { SITE } from '../../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
## Cloudflare
5+
6+
The [`@opennextjs/cloudflare`](https://www.npmjs.com/package/@opennextjs/cloudflare) adapter lets you deploy Next.js apps to [Cloudflare Workers](https://developers.cloudflare.com/workers) using the [Node.js "runtime" from Next.js](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes).
7+
8+
<Callout>
9+
[`@opennextjs/cloudflare`](https://www.npmjs.com/package/@opennextjs/cloudflare) is pre 1.0, and still in active development. You should try it, [report bugs](https://github.com/opennextjs/opennextjs-cloudflare/issues), [share feedback](https://github.com/opennextjs/opennextjs-cloudflare/discussions), and contribute code to help make running Next.js apps on Cloudflare easier. We don't quite yet recommend using it for mission-critical production apps.
10+
11+
You can also use [`@cloudflare/next-on-pages`](https://www.npmjs.com/package/@cloudflare/next-on-pages) to deploy Next.js apps to Cloudflare Pages. You can review the differences in supported Next.js features below and by reviewing [the docs for `@cloudflare/next-on-pages`](https://developers.cloudflare.com/pages/framework-guides/nextjs/ssr/supported-features/), and understand the differences between Workers and Pages [here](https://developers.cloudflare.com/workers/static-assets/compatibility-matrix/).
12+
13+
</Callout>
14+
15+
### Get Started
16+
17+
##### New apps
18+
19+
To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run:
20+
21+
```
22+
npm create cloudflare@latest -- my-next-app --framework=next --experimental
23+
```
24+
25+
##### Existing Next.js apps
26+
27+
Follow the guide [here](/cloudflare/0.2/get-started) to use [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) with an existing Next.js app.
28+
29+
### Supported Next.js runtimes
30+
31+
Next.js has [two "runtimes"](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes) — "Edge" and "Node.js". When you use `@opennextjs/cloudflare`, your app can use the Node.js runtime, which is more fully featured, and allows you to use the [Node.js APIs](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) that are provided by the Cloudflare Workers runtime.
32+
33+
This is an important difference from `@cloudflare/next-on-pages`, which only supports the "Edge" runtime. The Edge Runtime code in Next.js [intentionally constrains which APIs from Node.js can be used](https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/plugins/middleware-plugin.ts#L820), and the "Edge" runtime does not support all Next.js features.
34+
35+
### Supported Next.js versions
36+
37+
`@opennextjs/cloudflare` is pre 1.0, and still in active development. We intend to support all minor and patch version of Next.js 13 and 14, as well as Next.js 15 when it is released. (currently a release candidate)
38+
39+
To help improve compatibility, we encourage you to [report bugs](https://github.com/opennextjs/opennextjs-cloudflare/issues) and contribute code!
40+
41+
### Supported Next.js features
42+
43+
- [x] [App Router](https://nextjs.org/docs/app)
44+
- [x] [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)
45+
- [x] [Dynamic routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes)
46+
- [x] [Static Site Generation (SSG)](https://nextjs.org/docs/app/building-your-application/rendering/server-components#static-rendering-default)
47+
- [x] [Server-Side Rendering (SSR)](https://nextjs.org/docs/app/building-your-application/rendering/server-components)
48+
49+
### Not Yet Supported Next.js features
50+
51+
The following Next.js features are not yet supported — but we welcome both contributions and feedback! Tell us what you'd like to see, or what you'd like to add support for:
52+
53+
- [ ] [Pages Router](https://nextjs.org/docs/pages) (you should use the App Router instead, which was introduced in Next.js 13)
54+
- [ ] [Incremental Static Regeneration (ISR)](https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration)
55+
- [ ] [Partial Prerendering (PPR)](https://nextjs.org/docs/app/building-your-application/rendering/partial-prerendering)
56+
- [ ] [Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware)
57+
- [ ] [Image optimization](https://nextjs.org/docs/app/building-your-application/optimizing/images) (you can integrate Cloudflare Images with Next.js by following [this guide](https://developers.cloudflare.com/images/transform-images/integrate-with-frameworks/))
58+
- [ ] [Experimental streaming support](https://nextjs.org/blog/next-15-rc#executing-code-after-a-response-with-nextafter-experimental)
59+
60+
### How @opennextjs/cloudflare Works
61+
62+
The OpenNext Cloudflare adapter works by taking the Next.js build output and transforming it, so that it can run in Cloudflare Workers.
63+
64+
When you add [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) as a dependency to your Next.js app, and then run `npx cloudflare` the adapter first builds your app by running `next build`, and then transforms the build output to a format that you can run locally using [Wrangler](https://developers.cloudflare.com/workers/wrangler/), and deploy to Cloudflare.
65+
66+
You can view the code for @opennextjs/cloudflare [here](https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/src) to understand what it does under the hood.

pages/cloudflare/_meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"bindings": "",
55
"caching": "",
66
"examples": "",
7-
"troubleshooting": ""
7+
"troubleshooting": "",
8+
"migrate-from-0.2": "",
9+
"0.2": "Release 0.2"
810
}

0 commit comments

Comments
 (0)