Skip to content

Commit 8de2c04

Browse files
introduce new initOpenNextCloudflareForDev utility and make getCloudflareContext synchronous (#265)
Co-authored-by: Victor Berchet <[email protected]>
1 parent 4983a36 commit 8de2c04

23 files changed

+361
-206
lines changed

.changeset/chilly-dryers-begin.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
introduce new `initOpenNextCloudflareForDev` utility and make `getCloudflareContext` synchronous
6+
7+
this change introduces a new `initOpenNextCloudflareForDev` function that must called in the [Next.js config file](https://nextjs.org/docs/app/api-reference/config/next-config-js) to integrate the Next.js dev server with the open-next Cloudflare adapter.
8+
9+
Also makes `getCloudflareContext` synchronous.
10+
11+
Additionally the `getCloudflareContext` can now work during local development (`next dev`) in the edge runtime (including middlewares).
12+
13+
Moving forward we'll recommend that all applications include the use of the `initOpenNextCloudflareForDev` utility in their config file (there is no downside in doing so and it only effect local development).
14+
15+
Example:
16+
17+
```js
18+
// next.config.mjs
19+
20+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
21+
22+
initOpenNextCloudflareForDev();
23+
24+
/** @type {import('next').NextConfig} */
25+
const nextConfig = {};
26+
27+
export default nextConfig;
28+
```

examples/api/app/api/hello/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export async function GET() {
1111
return new Response("Hello World!");
1212
}
1313

14-
// Retrieve the bindings defined in wrangler.toml
15-
const { env } = await getCloudflareContext();
16-
return new Response(env.hello);
14+
// Retrieve the bindings defined in wrangler.json
15+
return new Response(getCloudflareContext().env.hello);
1716
}
1817

1918
export async function POST(request: Request) {

examples/api/e2e/playwright.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig, devices } from "@playwright/test";
2+
import type nodeProcess from "node:process";
23

3-
declare var process: { env: Record<string, string> };
4+
declare const process: typeof nodeProcess;
45

56
/**
67
* See https://playwright.dev/docs/test-configuration.
@@ -49,5 +50,6 @@ export default defineConfig({
4950
command: "pnpm preview:worker",
5051
url: "http://localhost:8770",
5152
reuseExistingServer: !process.env.CI,
53+
timeout: 70_000,
5254
},
5355
});

examples/api/e2e/playwright.dev.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig, devices } from "@playwright/test";
2+
import type nodeProcess from "node:process";
23

3-
declare var process: { env: Record<string, string> };
4+
declare const process: typeof nodeProcess;
45

56
/**
67
* See https://playwright.dev/docs/test-configuration.

examples/api/next.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
2+
3+
initOpenNextCloudflareForDev();
4+
15
/** @type {import('next').NextConfig} */
26
const nextConfig = {};
37

examples/create-next-app/e2e/playwright.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig, devices } from "@playwright/test";
2+
import type nodeProcess from "node:process";
23

3-
declare const process: { env: Record<string, string> };
4+
declare const process: typeof nodeProcess;
45

56
/**
67
* See https://playwright.dev/docs/test-configuration.
@@ -49,5 +50,6 @@ export default defineConfig({
4950
command: "pnpm preview:worker",
5051
url: "http://localhost:8771",
5152
reuseExistingServer: !process.env.CI,
53+
timeout: 70_000,
5254
},
5355
});

examples/create-next-app/next.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
2+
3+
initOpenNextCloudflareForDev();
4+
15
/** @type {import('next').NextConfig} */
26
const nextConfig = {};
37

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
import { headers } from "next/headers";
2+
13
export default function MiddlewarePage() {
2-
return <h1>Via middleware</h1>;
4+
const cloudflareContextHeader = headers().get("x-cloudflare-context");
5+
6+
return (
7+
<>
8+
<h1>Via middleware</h1>
9+
<p>
10+
The value of the <i>x-cloudflare-context</i> header is: <br />
11+
<span
12+
style={{
13+
display: "inline-block",
14+
margin: "1rem 2rem",
15+
color: "grey",
16+
fontSize: "1.2rem",
17+
}}
18+
data-testid="cloudflare-context-header"
19+
>
20+
{cloudflareContextHeader}
21+
</span>
22+
</p>
23+
</>
24+
);
325
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test("middlewares have access to the cloudflare context", async ({ page }) => {
4+
await page.goto("/middleware");
5+
const cloudflareContextHeaderElement = page.getByTestId("cloudflare-context-header");
6+
expect(await cloudflareContextHeaderElement.textContent()).toContain(
7+
"typeof `cloudflareContext.env` = object"
8+
);
9+
});

examples/middleware/e2e/playwright.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig, devices } from "@playwright/test";
2+
import type nodeProcess from "node:process";
23

3-
declare const process: { env: Record<string, string> };
4+
declare const process: typeof nodeProcess;
45

56
/**
67
* See https://playwright.dev/docs/test-configuration.
@@ -49,5 +50,6 @@ export default defineConfig({
4950
command: "pnpm preview:worker",
5051
url: "http://localhost:8774",
5152
reuseExistingServer: !process.env.CI,
53+
timeout: 70_000,
5254
},
5355
});

0 commit comments

Comments
 (0)