Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pages/cloudflare/bindings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ export async function GET(request) {

</Callout>

<Callout type='warning'>
In a page or layout (as opposed to a route handler), `getCloudflareContext()` can only be used if the route renders dynamically. If Next.js is able to statically prerender the route, `next build` will try to do so and fail (or the context will reflect build time rather than the incoming request).

Force the route to render dynamically by awaiting one of Next.js's [Dynamic APIs](https://nextjs.org/docs/app/api-reference/functions/headers) before calling `getCloudflareContext()`:
```js
import { headers } from "next/headers";
import { getCloudflareContext } from "@opennextjs/cloudflare";

export default async function Page() {
await headers(); // forces dynamic rendering
const myKv = getCloudflareContext().env.MY_KV_NAMESPACE;
// ...
}
```

Alternatively, export `export const dynamic = "force-dynamic";` from the page or layout.
</Callout>

#### 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/).
Expand Down