Skip to content

Commit 0de0116

Browse files
author
Juul
committed
feat: allow overriding platform proxy environment
1 parent ee1712e commit 0de0116

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

.changeset/shaggy-bobcats-battle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ initOpenNextCloudflareForDev({
1414
});
1515
```
1616

17-
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).
17+
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.

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,17 @@ async function getCloudflareContextAsync<
210210
* Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed
211211
* @param options options on how the function should operate and if/where to persist the platform data
212212
*/
213-
export async function initOpenNextCloudflareForDev(options?: Omit<GetPlatformProxyOptions, "environment">) {
213+
export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions) {
214214
const shouldInitializationRun = shouldContextInitializationRun();
215215
if (!shouldInitializationRun) return;
216216

217+
if (options?.environment && process.env.NEXT_DEV_WRANGLER_ENV) {
218+
console.warn(
219+
`'initOpenNextCloudflareForDev' has been called with an environment option while NEXT_DEV_WRANGLER_ENV is set.` +
220+
` NEXT_DEV_WRANGLER_ENV will be ignored and the environment will be set to: '${options.environment}'`
221+
);
222+
}
223+
217224
const context = await getCloudflareContextFromWrangler(options);
218225

219226
addCloudflareContextToNodejsGlobal(context);
@@ -293,13 +300,16 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte
293300
async function getCloudflareContextFromWrangler<
294301
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
295302
Context = ExecutionContext,
296-
>(options?: Omit<GetPlatformProxyOptions, "environment">): Promise<CloudflareContext<CfProperties, Context>> {
303+
>(options?: GetPlatformProxyOptions): Promise<CloudflareContext<CfProperties, Context>> {
297304
// Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does
298305
const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`);
306+
307+
// This allows the selection of a wrangler environment while running in next dev mode
308+
const environment = options?.environment ?? process.env.NEXT_DEV_WRANGLER_ENV;
309+
299310
const { env, cf, ctx } = await getPlatformProxy({
300-
// This allows the selection of a wrangler environment while running in next dev mode
301311
...options,
302-
environment: process.env.NEXT_DEV_WRANGLER_ENV,
312+
environment,
303313
});
304314
return {
305315
env,

0 commit comments

Comments
 (0)