Skip to content

Commit d235af4

Browse files
committed
Add docs for Cloudflare adapter
1 parent b6cafea commit d235af4

File tree

6 files changed

+228
-5
lines changed

6 files changed

+228
-5
lines changed

pages/aws/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ If you're migrating from V2 to V3, you can find the migration guide [here](/migr
1313
---
1414

1515
OpenNext takes the Next.js build output and converts it into packages that can be deployed across a variety of environments.
16-
Natively OpenNext has support for AWS Lambda and classic Node Server. It also offer partial support for the `edge` runtime in Cloudflare Workers.
16+
Natively OpenNext has support for AWS Lambda, [Cloudflare](/cloudflare), and classic Node.js Server.
1717

1818
One notable feature of OpenNext is its ability to split the Next.js output, enabling selective deployment to different targets such as AWS Lambda, Cloudflare Workers, or Amazon ECS. This facilitates a tailored deployment strategy that aligns with the specific needs of your application.
1919

pages/cloudflare/bindings.mdx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
<Callout>
9+
If your app uses both bindings and @opennextjs/cloudflare, you currently cannot access bindings when running `next dev`. Instead, you must build your app using @opennextjs/cloudflare and then run it locally in the Workers runtime as described [here](/cloudflare/get-started). This means you cannot use hot-module reloading (HMR) for a faster development workflow. @opennextjs/cloudflare is pre 1.0, in active development, and we plan to address this soon.
10+
11+
Alternatively, you can 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 between @opennextjs/cloudflare and @cloudflare/next-on-pages [here](LINK).
12+
</Callout>
13+
14+
#### How to configure your Next.js app so it can access bindings
15+
16+
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), y
17+
18+
#### How to access bindings in your Next.js app
19+
20+
You can access [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) from any route of your Next.js app via `getRequestContext`:
21+
22+
```js
23+
// TODO: possible to bring this in from next-on-pages?
24+
import { getRequestContext } from "@cloudflare/next-on-pages";
25+
26+
export async function GET(request) {
27+
let responseText = "Hello World";
28+
29+
const myKv = getRequestContext().env.MY_KV_NAMESPACE;
30+
await myKv.put("foo", "bar");
31+
const foo = await myKv.get("foo");
32+
33+
return new Response(foo);
34+
}
35+
```
36+
37+
#### How to add bindings to your Worker
38+
39+
Add bindings to your Worker by [adding them to your `wrangler.toml` configuration file](/pages/functions/wrangler-configuration/).
40+
41+
## TypeScript type declarations for bindings
42+
43+
To ensure that the `env` object from `getRequestContext().env` above has accurate TypeScript types, install [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) and create a [TypeScript declaration file](https://www.typescriptlang.org/docs/handbook/2/type-declarations.html).
44+
45+
Install Workers Types:
46+
47+
```sh
48+
npm install --save-dev @cloudflare/workers-types
49+
```
50+
51+
Add Workers Types to your `tsconfig.json` file, replacing the date below with your project's [compatibility date](https://developers.cloudflare.com/workers/configuration/compatibility-dates/):
52+
53+
```diff title="tsconfig.json"
54+
"types": [
55+
+ "@cloudflare/workers-types/2024-07-29"
56+
]
57+
```
58+
59+
Create an `env.d.ts` file in the root directory of your Next.js app, and explicitly declare the type of each binding:
60+
61+
```ts title="env.d.ts"
62+
interface CloudflareEnv {
63+
MY_KV_1: KVNamespace;
64+
MY_KV_2: KVNamespace;
65+
MY_R2: R2Bucket;
66+
MY_DO: DurableObjectNamespace;
67+
}
68+
```
69+
70+
## Other Cloudflare APIs (`cf`, `ctx`)
71+
72+
You can access context about the incoming request from the [`cf` object](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), as well as [lifecycle methods from the `ctx` object](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#lifecycle-methods) from the return value of [`getRequestContext()`](https://github.com/cloudflare/next-on-pages/blob/main/packages/next-on-pages/src/api/getRequestContext.ts):
73+
74+
```js
75+
// TODO: possible to bring this in from next-on-pages?
76+
import { getRequestContext } from "@cloudflare/next-on-pages";
77+
78+
export const runtime = "edge";
79+
80+
export async function GET(request) {
81+
const { env, cf, ctx } = getRequestContext();
82+
83+
// ...
84+
}
85+
```

pages/cloudflare/caching.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { SITE } from '../../config';
2+
import { Callout } from 'nextra/components';
3+
4+
## Caching
5+
6+
TODO

pages/cloudflare/get_started.mdx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
<command>
12+
```
13+
14+
#### Existing Next.js apps
15+
16+
##### 1. Install @cloudflare/next-on-pages
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. Add a `wrangler.toml` file
25+
26+
Then, add a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/) file to the root directory of your Next.js app:
27+
28+
```toml
29+
name = "my-app"
30+
compatibility_date = "2024-07-29"
31+
compatibility_flags = ["nodejs_compat"]
32+
assets = { directory = "public"}
33+
```
34+
35+
`wrangler.toml` is where you configure your Worker and define what resources it can access via [bindings](/workers/runtime-apis/bindings/).
36+
37+
##### 3. Update `next.config.mjs`
38+
39+
Next, update the content in your `next.config.mjs` file.
40+
41+
```diff title="next.config.mjs"
42+
+ import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev';
43+
44+
/** @type {import('next').NextConfig} */
45+
const nextConfig = {};
46+
47+
+ if (process.env.NODE_ENV === 'development') {
48+
+ await setupDevPlatform();
49+
+ }
50+
51+
export default nextConfig;
52+
```
53+
54+
These changes allows you to access [bindings](/pages/framework-guides/nextjs/ssr/bindings/) in local development.
55+
56+
##### 5. Update `package.json`
57+
58+
Add the following to the scripts field of your `package.json` file:
59+
60+
```json title="package.json"
61+
"build:worker": "cloudflare", TODO
62+
"dev:worker": "wrangler dev --port 8771",
63+
"preview:worker": "npm run build:worker && npm run dev:worker",
64+
"deploy:worker": "npm run build:worker && wrangler deploy"
65+
```
66+
67+
- `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.
68+
- `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.
69+
- `npm run preview:worker`: Runs `build:worker` and then `preview:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command.
70+
- `npm run deploy`: Builds your app, and then deploys it to Cloudflare
71+
72+
##### 6. Deploy to Cloudflare Workers
73+
74+
Either deploy via the command line:
75+
76+
```sh
77+
npm run deploy
78+
```
79+
80+
Or [connect a Github or Gitlab repository](https://develoers.cloudflare.com/workers/ci-cd/), and Cloudflare will automatically build and deploy each pull request you merge to your production branch.

pages/cloudflare/index.mdx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1-
### TODO
1+
import { SITE } from '../../config';
2+
import { Callout } from 'nextra/components';
23

4+
## Cloudflare
5+
6+
---
7+
8+
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) and [Cloudflare Pages](https://developers.cloudflare.com/pages).
9+
10+
---
11+
12+
<Callout>
13+
[@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), and [share feedback](https://github.com/opennextjs/opennextjs-cloudflare/discussions) to help make running Next.js apps on Cloudflare easier. We don't quite yet recommend using it for mission-critical production apps.
14+
15+
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 between @opennextjs/cloudflare and @cloudflare/next-on-pages [here](LINK).
16+
</Callout>
17+
18+
### Get Started
19+
20+
##### New apps
21+
22+
To create a new Next.js app, pre-configured to run on Cloudflare using @opennextjs/cloudflare, run:
23+
24+
```
25+
<command>
26+
```
27+
28+
##### Existing Next.js apps
29+
30+
Follow the guide [here](/cloudflare/get-started) to use [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) with an existing Next.js app.
31+
32+
### Supported Next.js features
33+
34+
TODO: Check these, what else should / shouldn't be on here
35+
36+
- [x] App & Pages Router
37+
- [x] API routes
38+
- [x] Dynamic routes
39+
- [x] Static site generation (SSG)
40+
- [x] Server-side rendering (SSR)
41+
- [x] Incremental static regeneration (ISR)
42+
- [x] Partial Prerendering (PPR)
43+
- [x] Middleware
44+
- [x] Image optimization
45+
- [ ] [NextAuth.js](https://next-auth.js.org)
46+
- [x] Experimental streaming support
47+
48+
### How @opennextjs/cloudflare Works
49+
50+
The OpenNext Cloudflare adapter works by taking the Next.js build output and transforming it, so that it can run in Cloudflare Workers.
51+
52+
When you add [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) as a dependency to your Next.js app, and then run `pnpx cloudflare` (TODO: why pnpx?), 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.
53+
54+
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/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SITE } from '../config';
22

33
# OpenNext
44

5-
Next.js, unlike Remix, Astro, or the other modern frontends, doesn't have a way to self-host across different platforms. You can run it as a Node application. But this doesn't work the same way as it does on Vercel.
5+
Next.js, unlike Remix, Astro, or the other modern frontends, doesn't have a way to self-host across different platforms. You can run it as a Node.js application. But this doesn't work the same way as it does on Vercel.
66

77
---
88

@@ -14,10 +14,10 @@ OpenNext is an initiative to bring all these efforts together.
1414

1515
---
1616

17-
OpenNext is currently backed by.
17+
OpenNext is currently backed by:
1818

1919
1. [SST](https://sst.dev) community, maintains the [AWS](aws) adapter
20-
2. [Cloudflare](https://www.cloudflare.com) team, maintains the [Cloudflare](cloudflare) adapter
20+
2. [Cloudflare](https://developers.cloudflare.com/) team, maintains the [Cloudflare](cloudflare) adapter
2121
3. [Netlify](http://netlify.com) team, maintains the [Netlify](netlify) adapter
2222

2323
If you'd like to join the effort, connect with us on [Discord](https://discord.gg/opennextjs).

0 commit comments

Comments
 (0)