From ac8dd81554fad902c4990ddf0da4fded46747924 Mon Sep 17 00:00:00 2001 From: Juul Damen Date: Mon, 17 Mar 2025 14:35:31 +0100 Subject: [PATCH 1/7] feat: support passing options to platform proxy --- packages/cloudflare/src/api/cloudflare-context.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index 279b74f5..6771c117 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -1,5 +1,7 @@ import type { Context, RunningCodeOptions } from "node:vm"; +import type { GetPlatformProxyOptions } from "wrangler"; + import type { DurableObjectQueueHandler } from "./durable-objects/queue"; import { DOShardedTagCache } from "./durable-objects/sharded-tag-cache"; @@ -290,12 +292,13 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte async function getCloudflareContextFromWrangler< CfProperties extends Record = IncomingRequestCfProperties, Context = ExecutionContext, ->(): Promise> { +>(options?: Omit): Promise> { // Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`); const { env, cf, ctx } = await getPlatformProxy({ // This allows the selection of a wrangler environment while running in next dev mode environment: process.env.NEXT_DEV_WRANGLER_ENV, + ...options, }); return { env, From ad7a39181038d7661ac3bb68016edf80018c06a2 Mon Sep 17 00:00:00 2001 From: Juul Damen Date: Mon, 17 Mar 2025 14:51:18 +0100 Subject: [PATCH 2/7] fix: support passing options to init open-next cloudflare dev function --- packages/cloudflare/src/api/cloudflare-context.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index 6771c117..bfd4b203 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -208,12 +208,13 @@ async function getCloudflareContextAsync< * with the open-next Cloudflare adapter * * Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed + * @param options options how the function should operate and if/where to persist the platform data */ -export async function initOpenNextCloudflareForDev() { +export async function initOpenNextCloudflareForDev(options?: Omit) { const shouldInitializationRun = shouldContextInitializationRun(); if (!shouldInitializationRun) return; - const context = await getCloudflareContextFromWrangler(); + const context = await getCloudflareContextFromWrangler(options); addCloudflareContextToNodejsGlobal(context); From 417be7d1d13eff0e554490591a89b6db74e13284 Mon Sep 17 00:00:00 2001 From: Juul Damen Date: Mon, 17 Mar 2025 15:06:27 +0100 Subject: [PATCH 3/7] chore: add changeset --- .changeset/shaggy-bobcats-battle.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .changeset/shaggy-bobcats-battle.md diff --git a/.changeset/shaggy-bobcats-battle.md b/.changeset/shaggy-bobcats-battle.md new file mode 100644 index 00000000..b93017bd --- /dev/null +++ b/.changeset/shaggy-bobcats-battle.md @@ -0,0 +1,15 @@ +--- +"@opennextjs/cloudflare": patch +--- + +Adds support for passing options to initOpenNextCloudflareForDev(). + +For example: + +```ts +initOpenNextCloudflareForDev({ + persist: { + path: "../../.wrangler/state/v3/custom-dir", + }, +}); +``` From 2b02f471409d438cc8a001e8d467d8c7fe5b57aa Mon Sep 17 00:00:00 2001 From: Juul <31408341+Juuldamen@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:36:04 +0100 Subject: [PATCH 4/7] fix: change platform proxy options order Co-authored-by: Victor Berchet --- packages/cloudflare/src/api/cloudflare-context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index bfd4b203..3f0053f8 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -298,8 +298,8 @@ async function getCloudflareContextFromWrangler< const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`); const { env, cf, ctx } = await getPlatformProxy({ // This allows the selection of a wrangler environment while running in next dev mode - environment: process.env.NEXT_DEV_WRANGLER_ENV, ...options, + environment: process.env.NEXT_DEV_WRANGLER_ENV, }); return { env, From 97be67a9b789c91bd19c46f112edb375e12f91c6 Mon Sep 17 00:00:00 2001 From: Juul <> Date: Tue, 18 Mar 2025 12:12:03 +0100 Subject: [PATCH 5/7] chore: fix prettier style issue --- packages/cloudflare/src/api/cloudflare-context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index 3f0053f8..85dcbe75 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -299,7 +299,7 @@ async function getCloudflareContextFromWrangler< const { env, cf, ctx } = await getPlatformProxy({ // This allows the selection of a wrangler environment while running in next dev mode ...options, - environment: process.env.NEXT_DEV_WRANGLER_ENV, + environment: process.env.NEXT_DEV_WRANGLER_ENV, }); return { env, From ee1712e2444a15a7c6b4e3d6504b5a7799ecaade Mon Sep 17 00:00:00 2001 From: Juul <> Date: Fri, 21 Mar 2025 10:29:02 +0100 Subject: [PATCH 6/7] chore: enhance changeset docs & jsdoc --- .changeset/shaggy-bobcats-battle.md | 6 ++++-- packages/cloudflare/src/api/cloudflare-context.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.changeset/shaggy-bobcats-battle.md b/.changeset/shaggy-bobcats-battle.md index b93017bd..d651d39a 100644 --- a/.changeset/shaggy-bobcats-battle.md +++ b/.changeset/shaggy-bobcats-battle.md @@ -2,9 +2,9 @@ "@opennextjs/cloudflare": patch --- -Adds support for passing options to initOpenNextCloudflareForDev(). +Adds support for passing options to `initOpenNextCloudflareForDev()`. This allows you to configure how your Cloudflare bindings will behave during [local development](https://opennext.js.org/cloudflare/get-started#11-develop-locally). -For example: +For example, the below configuration will persist the local state of bindings to a custom directory. Which can be useful if you want to share the state between different apps that reuse the same bindings in a monorepo. ```ts initOpenNextCloudflareForDev({ @@ -13,3 +13,5 @@ initOpenNextCloudflareForDev({ }, }); ``` + +You can find the type with the available options [here](https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/api/integrations/platform/index.ts#L32) in the Wrangler source code. Please note that the `environment` field is not customizable as it's used internally by OpenNext.js. If you have a use case where overriding `environment` is necessary, please let us know by opening a [feature request](https://github.com/cloudflare/workers-sdk/issues). diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index 85dcbe75..dd2ab460 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -208,7 +208,7 @@ async function getCloudflareContextAsync< * with the open-next Cloudflare adapter * * Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed - * @param options options how the function should operate and if/where to persist the platform data + * @param options options on how the function should operate and if/where to persist the platform data */ export async function initOpenNextCloudflareForDev(options?: Omit) { const shouldInitializationRun = shouldContextInitializationRun(); From 0de0116d4ebb2ad83f567ac71236137399085011 Mon Sep 17 00:00:00 2001 From: Juul <> Date: Thu, 27 Mar 2025 12:31:39 +0100 Subject: [PATCH 7/7] feat: allow overriding platform proxy environment --- .changeset/shaggy-bobcats-battle.md | 2 +- .../cloudflare/src/api/cloudflare-context.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.changeset/shaggy-bobcats-battle.md b/.changeset/shaggy-bobcats-battle.md index d651d39a..489a389f 100644 --- a/.changeset/shaggy-bobcats-battle.md +++ b/.changeset/shaggy-bobcats-battle.md @@ -14,4 +14,4 @@ initOpenNextCloudflareForDev({ }); ``` -You can find the type with the available options [here](https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/api/integrations/platform/index.ts#L32) in the Wrangler source code. Please note that the `environment` field is not customizable as it's used internally by OpenNext.js. If you have a use case where overriding `environment` is necessary, please let us know by opening a [feature request](https://github.com/cloudflare/workers-sdk/issues). +You can find the configuration type with it's available options [here](https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/api/integrations/platform/index.ts#L32) in the Wrangler source code. diff --git a/packages/cloudflare/src/api/cloudflare-context.ts b/packages/cloudflare/src/api/cloudflare-context.ts index dd2ab460..367eb07a 100644 --- a/packages/cloudflare/src/api/cloudflare-context.ts +++ b/packages/cloudflare/src/api/cloudflare-context.ts @@ -210,10 +210,17 @@ async function getCloudflareContextAsync< * Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed * @param options options on how the function should operate and if/where to persist the platform data */ -export async function initOpenNextCloudflareForDev(options?: Omit) { +export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions) { const shouldInitializationRun = shouldContextInitializationRun(); if (!shouldInitializationRun) return; + if (options?.environment && process.env.NEXT_DEV_WRANGLER_ENV) { + console.warn( + `'initOpenNextCloudflareForDev' has been called with an environment option while NEXT_DEV_WRANGLER_ENV is set.` + + ` NEXT_DEV_WRANGLER_ENV will be ignored and the environment will be set to: '${options.environment}'` + ); + } + const context = await getCloudflareContextFromWrangler(options); addCloudflareContextToNodejsGlobal(context); @@ -293,13 +300,16 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte async function getCloudflareContextFromWrangler< CfProperties extends Record = IncomingRequestCfProperties, Context = ExecutionContext, ->(options?: Omit): Promise> { +>(options?: GetPlatformProxyOptions): Promise> { // Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`); + + // This allows the selection of a wrangler environment while running in next dev mode + const environment = options?.environment ?? process.env.NEXT_DEV_WRANGLER_ENV; + const { env, cf, ctx } = await getPlatformProxy({ - // This allows the selection of a wrangler environment while running in next dev mode ...options, - environment: process.env.NEXT_DEV_WRANGLER_ENV, + environment, }); return { env,