Skip to content

Commit aef8e51

Browse files
authored
feat: support passing options to init open-next cloudflare dev function (#469)
1 parent fafadaa commit aef8e51

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

.changeset/shaggy-bobcats-battle.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
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).
6+
7+
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.
8+
9+
```ts
10+
initOpenNextCloudflareForDev({
11+
persist: {
12+
path: "../../.wrangler/state/v3/custom-dir",
13+
},
14+
});
15+
```
16+
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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Context, RunningCodeOptions } from "node:vm";
22

3+
import type { GetPlatformProxyOptions } from "wrangler";
4+
35
import type { DurableObjectQueueHandler } from "./durable-objects/queue";
46
import { DOShardedTagCache } from "./durable-objects/sharded-tag-cache";
57

@@ -206,12 +208,20 @@ async function getCloudflareContextAsync<
206208
* with the open-next Cloudflare adapter
207209
*
208210
* 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
209212
*/
210-
export async function initOpenNextCloudflareForDev() {
213+
export async function initOpenNextCloudflareForDev(options?: GetPlatformProxyOptions) {
211214
const shouldInitializationRun = shouldContextInitializationRun();
212215
if (!shouldInitializationRun) return;
213216

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);
215225

216226
addCloudflareContextToNodejsGlobal(context);
217227

@@ -290,12 +300,16 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte
290300
async function getCloudflareContextFromWrangler<
291301
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
292302
Context = ExecutionContext,
293-
>(): Promise<CloudflareContext<CfProperties, Context>> {
303+
>(options?: GetPlatformProxyOptions): Promise<CloudflareContext<CfProperties, Context>> {
294304
// Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does
295305
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+
296310
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,
299313
});
300314
return {
301315
env,

0 commit comments

Comments
 (0)