Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Dario Piotrowicz <[email protected]>
  • Loading branch information
irvinebroque and dario-piotrowicz authored Sep 26, 2024
1 parent 05933a8 commit b30aec3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions pages/cloudflare/bindings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloud

#### 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 `getRequestContext`:
You can access [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/) from any route of your Next.js app via `getCloudlfareContext`:

```js
// TODO: possible to bring this in from next-on-pages?
Expand All @@ -26,7 +26,7 @@ import { getRequestContext } from "@cloudflare/next-on-pages";
export async function GET(request) {
let responseText = "Hello World";

const myKv = getRequestContext().env.MY_KV_NAMESPACE;
const myKv = (await getCloudflareContext()).env.MY_KV_NAMESPACE;
await myKv.put("foo", "bar");
const foo = await myKv.get("foo");

Expand All @@ -40,7 +40,7 @@ Add bindings to your Worker by [adding them to your `wrangler.toml` configuratio

## TypeScript type declarations for bindings

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).
To ensure that the `env` object from `(await getCloudflareContext()).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).

Install Workers Types:

Expand Down Expand Up @@ -69,16 +69,14 @@ interface CloudflareEnv {

## 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/#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):
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
// TODO: possible to bring this in from next-on-pages?
import { getRequestContext } from "@cloudflare/next-on-pages";
import { getCloudflareContext } from "@opennextjs/cloudflare";

export const runtime = "edge";

export async function GET(request) {
const { env, cf, ctx } = getRequestContext();
const { env, cf, ctx } = await getCloudflareContext();

// ...
}
Expand Down
2 changes: 1 addition & 1 deletion pages/cloudflare/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To create a new Next.js app, pre-configured to run on Cloudflare using @opennext

#### Existing Next.js apps

##### 1. Install @cloudflare/next-on-pages
##### 1. Install @opennextjs/cloudflare

First, install [@opennextjs/cloudflare](https://www.npmjs.com/package/@opennextjs/cloudflare):

Expand Down

0 comments on commit b30aec3

Please sign in to comment.