-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 0.3 directory and migrate-from-0.3 page
- Loading branch information
1 parent
4fbbb91
commit 962fb8c
Showing
8 changed files
with
443 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"index": "Overview", | ||
"get-started": "", | ||
"bindings": "", | ||
"caching": "", | ||
"examples": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { SITE } from '../../../config'; | ||
import { Callout } from 'nextra/components'; | ||
|
||
### Bindings | ||
|
||
[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. | ||
|
||
#### How to configure your Next.js app so it can access bindings | ||
|
||
Install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare), and then add a [wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/) in the root directory of your Next.js app, as described in [Get Started](/cloudflare/get-started#3-create-a-wranglerjson-file). | ||
|
||
#### How to access bindings in your Next.js app | ||
|
||
You can access [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) from any route of your Next.js app via `getCloudflareContext`: | ||
|
||
```js | ||
import { getCloudflareContext } from "@opennextjs/cloudflare"; | ||
|
||
export async function GET(request) { | ||
let responseText = "Hello World"; | ||
|
||
const myKv = (await getCloudflareContext()).env.MY_KV_NAMESPACE; | ||
await myKv.put("foo", "bar"); | ||
const foo = await myKv.get("foo"); | ||
|
||
return new Response(foo); | ||
} | ||
``` | ||
|
||
#### How to add bindings to your Worker | ||
|
||
Add bindings to your Worker by adding them to your [wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/). | ||
|
||
## TypeScript type declarations for bindings | ||
|
||
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): | ||
|
||
``` | ||
npx wrangler types --experimental-include-runtime | ||
``` | ||
|
||
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. | ||
|
||
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: | ||
|
||
```bash | ||
npx wrangler types --experimental-include-runtime="./runtime.d.ts" | ||
``` | ||
|
||
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. | ||
|
||
## Other Cloudflare APIs (`cf`, `ctx`) | ||
|
||
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): | ||
|
||
```js | ||
import { getCloudflareContext } from "@opennextjs/cloudflare"; | ||
|
||
|
||
export async function GET(request) { | ||
const { env, cf, ctx } = await getCloudflareContext(); | ||
|
||
// ... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { SITE } from '../../../config'; | ||
import { Callout } from 'nextra/components'; | ||
|
||
## Caching | ||
|
||
`@opennextjs/cloudflare` supports [caching](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#caching-data). | ||
|
||
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. | ||
|
||
[The cache persists across deployments](https://nextjs.org/docs/app/building-your-application/caching#data-cache). You are responsible for revalidating/purging this cache. | ||
|
||
Note that [Revalidating](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) is not yet supported. | ||
|
||
Next.js primes the cache at build time. The build time values are serverd by the [Workers Assets](https://developers.cloudflare.com/workers/static-assets/). | ||
|
||
<Callout> | ||
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. | ||
</Callout> | ||
|
||
### How to enable caching | ||
|
||
`@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. | ||
|
||
To enable caching, you must: | ||
|
||
#### 1. Create a KV namespace | ||
|
||
``` | ||
npx wrangler@latest kv namespace create <YOUR_NAMESPACE_NAME> | ||
``` | ||
|
||
#### 2. Add the KV namespace to your Worker | ||
|
||
The binding name used in your app's worker is `NEXT_CACHE_WORKERS_KV`. | ||
|
||
```jsonc | ||
// wrangler.json | ||
{ | ||
// ... | ||
"kv_namespaces": [ | ||
{ | ||
"binding": "NEXT_CACHE_WORKERS_KV", | ||
"id": "<BINDING_ID>" | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { SITE } from '../../../config'; | ||
import { Callout } from 'nextra/components'; | ||
|
||
## Examples | ||
|
||
To create a new Next.js app, pre-configured to run on Cloudflare using `@opennextjs/cloudflare`, run: | ||
|
||
``` | ||
npm create cloudflare@latest -- my-next-app --framework=next --experimental | ||
``` | ||
|
||
### Basic starter projects | ||
|
||
Basic example apps are included in the repository for `@opennextjs/cloudflare` package: | ||
|
||
- [*`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). | ||
- [*`api`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/api) — a minimal Next.js project with a single API route | ||
- [*`middleware`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/middleware) — a minimal Next.js project using middleware | ||
- [*`vercel-blog-starter`*](https://github.com/opennextjs/opennextjs-cloudflare/tree/main/examples/vercel-blog-starter) — a blog project using SSG | ||
|
||
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). | ||
|
||
### Next.js Commerce Demo | ||
|
||
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import { SITE } from '../../../config'; | ||
import { Callout } from 'nextra/components'; | ||
|
||
### Get Started | ||
|
||
#### New apps | ||
|
||
To create a new Next.js app, pre-configured to run on Cloudflare using `@opennextjs/cloudflare`, run: | ||
|
||
``` | ||
npm create cloudflare@latest -- my-next-app --framework=next --experimental | ||
``` | ||
|
||
#### Existing Next.js apps | ||
|
||
##### 1. Install @opennextjs/cloudflare | ||
|
||
First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare): | ||
|
||
```sh | ||
npm install --save-dev @opennextjs/cloudflare@latest | ||
``` | ||
|
||
##### 2. Install Wrangler | ||
|
||
Install the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) as a devDependency: | ||
|
||
```npm | ||
npm install --save-dev wrangler@latest | ||
``` | ||
|
||
<Callout> | ||
You must use Wrangler version `3.99.0` or later to deploy Next.js apps using `@opennextjs/cloudflare`. | ||
</Callout> | ||
|
||
##### 3. Create a wrangler configuration file | ||
|
||
<Callout type='info'> | ||
This step is optional since `@opennextjs/cloudflare` creates this file for you during the build process (if not already present). | ||
</Callout> | ||
|
||
A [wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/) is needed for your | ||
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). | ||
|
||
You can create one yourself in the root directory of your Next.js app with the name `wrangler.json` and the following content: | ||
```jsonc | ||
{ | ||
"$schema": "node_modules/wrangler/config-schema.json", | ||
"main": ".open-next/worker.js", | ||
"name": "my-app", | ||
"compatibility_date": "2024-12-30", | ||
"compatibility_flags": ["nodejs_compat"], | ||
"assets": { | ||
"directory": ".open-next/assets", | ||
"binding": "ASSETS", | ||
}, | ||
"kv_namespaces": [ | ||
// Create a KV binding with the binding name "NEXT_CACHE_WORKERS_KV" | ||
// to enable the KV based caching: | ||
// { | ||
// "binding": "NEXT_CACHE_WORKERS_KV", | ||
// "id": "<BINDING_ID>" | ||
// } | ||
], | ||
} | ||
``` | ||
|
||
<Callout> | ||
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 | ||
- The `main` and `assets` values should also not be changed unless you modify the build output result in some way | ||
- 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) | ||
</Callout> | ||
|
||
##### 4. Add an `open-next.config.ts` file | ||
|
||
<Callout type='info'> | ||
This step is optional since `@opennextjs/cloudflare` creates this file for you during the build process (if not already present). | ||
</Callout> | ||
|
||
Add a [`open-next.config.ts`](https://opennext.js.org/aws/config) file to the root directory of your Next.js app: | ||
|
||
```ts | ||
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js"; | ||
import cache from "@opennextjs/cloudflare/kvCache"; | ||
|
||
const config: OpenNextConfig = { | ||
default: { | ||
override: { | ||
wrapper: "cloudflare-node", | ||
converter: "edge", | ||
// set `incrementalCache` to "dummy" to disable KV cache | ||
incrementalCache: async () => cache, | ||
tagCache: "dummy", | ||
queue: "dummy", | ||
}, | ||
}, | ||
|
||
middleware: { | ||
external: true, | ||
override: { | ||
wrapper: "cloudflare-edge", | ||
converter: "edge", | ||
proxyExternalRequest: "fetch", | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; | ||
``` | ||
|
||
<Callout> | ||
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. | ||
</Callout> | ||
|
||
##### 5. Add a `.dev.vars` file | ||
|
||
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: | ||
|
||
```text | ||
NEXTJS_ENV=development | ||
``` | ||
|
||
The `NEXTJS_ENV` variable defines the environment to use when loading Next.js `.env` files. It defaults to "production" when not defined. | ||
|
||
##### 6. Update the `package.json` file | ||
|
||
Add the following to the scripts field of your `package.json` file: | ||
|
||
```json | ||
"build:worker": "opennextjs-cloudflare", | ||
"dev:worker": "wrangler dev --port 8771", | ||
"preview": "npm run build:worker && npm run dev:worker", | ||
"deploy": "npm run build:worker && wrangler deploy", | ||
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts", | ||
``` | ||
|
||
- `npm run build:worker`: Runs the [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare) adapter. This first builds your app by running the `build` script in your `package.json` (Next.js apps use `next build` by default), 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. The build command used by OpenNext can be overridden with the `buildCommand` option in your OpenNext config. | ||
- `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. | ||
- `npm run preview`: Runs `build:worker` and then `dev:worker`, allowing you to quickly preview your app running locally in the Workers runtime, via a single command. | ||
- `npm run deploy`: Builds your app, and then deploys it to Cloudflare | ||
- `cf-typegen`: Generates a `cloudflare-env.d.ts` file at the root of your project containing [the types for the `env`](https://developers.cloudflare.com/workers/wrangler/commands/#types). | ||
|
||
##### 7. Add caching with Workers KV | ||
|
||
See the [Caching docs](/cloudflare/caching) for information on enabling Next.js caching in your OpenNext project. | ||
|
||
##### 8. Remove any `export const runtime = "edge";` if present | ||
|
||
Before deploying your app, remove the `export const runtime = "edge";` line from any of your source files. | ||
|
||
The edge runtime is not supported yet with `@opennextjs/cloudflare`. | ||
|
||
##### 9. Add `.open-next` to `.gitignore` | ||
|
||
You should add `.open-next` to your `.gitignore` file to prevent the build output from being committed to your repository. | ||
|
||
##### 10. Remove `@cloudflare/next-on-pages` (if necessary) | ||
|
||
If your Next.js app currently uses `@cloudflare/next-on-pages`, you'll want to remove it, and make a few changes. | ||
|
||
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. | ||
|
||
Remove any reference of these packages from your source and configuration files. | ||
This includes: | ||
- `setupDevPlatform()` calls in your Next.js config file | ||
- `getRequestContext` imports from `@cloudflare/next-on-pages` from your source files | ||
(those can be replaced with `getCloudflareContext` calls from `@opennextjs/cloudflare`) | ||
- next-on-pages eslint rules set in your Eslint config file | ||
|
||
##### 11. Develop locally | ||
|
||
You can continue to run `next dev` when developing locally. | ||
|
||
During local development, you can access local versions of Cloudflare bindings as indicated in the [bindings documentation](./bindings). | ||
|
||
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. | ||
|
||
##### 12. Deploy to Cloudflare Workers | ||
|
||
Either deploy via the command line: | ||
|
||
```sh | ||
npm run deploy:worker | ||
``` | ||
|
||
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. |
Oops, something went wrong.