diff --git a/pages/cloudflare/bindings.mdx b/pages/cloudflare/bindings.mdx index 5fd42db..ceb1eb9 100644 --- a/pages/cloudflare/bindings.mdx +++ b/pages/cloudflare/bindings.mdx @@ -35,6 +35,24 @@ export async function GET(request) { + + 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. + + #### 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/).