Skip to content

Commit b92bf3b

Browse files
committed
fix for initOpenNextCloudflareForDev
1 parent bdc95fb commit b92bf3b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

examples/e2e/app-router/next.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { NextConfig } from "next";
2+
const { initOpenNextCloudflareForDev } = require("@opennextjs/cloudflare");
3+
initOpenNextCloudflareForDev();
24

35
const nextConfig: NextConfig = {
46
poweredByHeader: false,

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,30 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte
282282
};
283283
}
284284

285+
async function patchWranglerConfig() {
286+
//First thing here, we try to load the `wrangler.jsonc` file
287+
let existingWranglerConfig: Record<string, unknown> | null = null;
288+
const originalWranglerConfigPath = `${process.cwd()}/wrangler.jsonc`;
289+
const patchedWranglerConfigPath = `${process.cwd()}/.open-next/wrangler.jsonc`;
290+
try {
291+
const fs = await import("node:fs/promises");
292+
//@ts-ignore
293+
existingWranglerConfig = JSON.parse(await fs.readFile(originalWranglerConfigPath, "utf-8"));
294+
if (!existingWranglerConfig) {
295+
console.error("If you use one of the DO overrides, you need to have a wrangler.jsonc file");
296+
return undefined;
297+
}
298+
delete existingWranglerConfig["durable_objects"];
299+
300+
// We need to ensure that the `.open-next` directory exists
301+
await fs.mkdir(`${process.cwd()}/.open-next`, { recursive: true });
302+
await fs.writeFile(patchedWranglerConfigPath, JSON.stringify(existingWranglerConfig));
303+
} catch {
304+
console.error("If you use one of the DO overrides, you need to have a wrangler.jsonc file");
305+
}
306+
return existingWranglerConfig ? patchedWranglerConfigPath : undefined;
307+
}
308+
285309
/**
286310
* Gets a cloudflare context object from wrangler
287311
*
@@ -291,11 +315,13 @@ async function getCloudflareContextFromWrangler<
291315
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
292316
Context = ExecutionContext,
293317
>(): Promise<CloudflareContext<CfProperties, Context>> {
318+
const configPath = await patchWranglerConfig();
294319
// Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does
295320
const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`);
296321
const { env, cf, ctx } = await getPlatformProxy({
297322
// This allows the selection of a wrangler environment while running in next dev mode
298323
environment: process.env.NEXT_DEV_WRANGLER_ENV,
324+
configPath,
299325
});
300326
return {
301327
env,

0 commit comments

Comments
 (0)