|
1 | 1 | import type { Context, RunningCodeOptions } from "node:vm";
|
2 | 2 |
|
| 3 | +import type { GetPlatformProxyOptions } from "wrangler"; |
| 4 | + |
3 | 5 | import type { DurableObjectQueueHandler } from "./durable-objects/queue";
|
4 | 6 | import { DOShardedTagCache } from "./durable-objects/sharded-tag-cache";
|
5 | 7 |
|
@@ -206,12 +208,20 @@ async function getCloudflareContextAsync<
|
206 | 208 | * with the open-next Cloudflare adapter
|
207 | 209 | *
|
208 | 210 | * Note: this function should only be called inside the Next.js config file, and although async it doesn't need to be `await`ed
|
| 211 | + * @param options options on how the function should operate and if/where to persist the platform data |
209 | 212 | */
|
210 |
| -export async function initOpenNextCloudflareForDev() { |
| 213 | +export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions) { |
211 | 214 | const shouldInitializationRun = shouldContextInitializationRun();
|
212 | 215 | if (!shouldInitializationRun) return;
|
213 | 216 |
|
214 |
| - const context = await getCloudflareContextFromWrangler(); |
| 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 | + |
| 224 | + const context = await getCloudflareContextFromWrangler(options); |
215 | 225 |
|
216 | 226 | addCloudflareContextToNodejsGlobal(context);
|
217 | 227 |
|
@@ -290,12 +300,16 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte
|
290 | 300 | async function getCloudflareContextFromWrangler<
|
291 | 301 | CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
|
292 | 302 | Context = ExecutionContext,
|
293 |
| ->(): Promise<CloudflareContext<CfProperties, Context>> { |
| 303 | +>(options?: GetPlatformProxyOptions): Promise<CloudflareContext<CfProperties, Context>> { |
294 | 304 | // Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does
|
295 | 305 | 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 | + |
296 | 310 | const { env, cf, ctx } = await getPlatformProxy({
|
297 |
| - // This allows the selection of a wrangler environment while running in next dev mode |
298 |
| - environment: process.env.NEXT_DEV_WRANGLER_ENV, |
| 311 | + ...options, |
| 312 | + environment, |
299 | 313 | });
|
300 | 314 | return {
|
301 | 315 | env,
|
|
0 commit comments