Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/e2e/app-router/next.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { NextConfig } from "next";
const { initOpenNextCloudflareForDev } = require("@opennextjs/cloudflare");
initOpenNextCloudflareForDev();

const nextConfig: NextConfig = {
poweredByHeader: false,
Expand Down
40 changes: 40 additions & 0 deletions packages/cloudflare/src/api/cloudflare-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,44 @@ async function monkeyPatchVmModuleEdgeContext(cloudflareContext: CloudflareConte
};
}

async function patchWranglerConfig() {
//First thing here, we try to load the `wrangler.jsonc` file
let existingWranglerConfig: Record<string, unknown> | null = null;
const originalWranglerConfigPath = `${process.cwd()}/wrangler.jsonc`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But:

  • this does not support toml and json extensions
  • this does not support custom config paths

I think the best workaround for now would be to set script_name to the worker name. It is not ideal either as wrangler then shows a misleading warning :(

ref: cloudflare/workers-sdk#8687

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it was more just a quick patch to test a fix for some cases and to get feedback (that's why it's a draft)

The big issue with the script_name is that in preview if you use one of the DO, it fails.
I think the good quick fix is in the workers-sdk to have an option to ignore DO for getPlatformProxy, we don't need more than that, just that it doesn't crash at build time.

const patchedWranglerConfigPath = `${process.cwd()}/.open-next/dev-wrangler.jsonc`;
try {
// We can't use `node:fs/promises` here in next 14 it seems
// eslint-disable-next-line unicorn/prefer-node-protocol
const fs = await import("fs/promises");
//@ts-ignore
existingWranglerConfig = JSON.parse(await fs.readFile(originalWranglerConfigPath, "utf-8"));
if (!existingWranglerConfig) {
console.error("If you use one of the DO overrides, you need to have a wrangler.jsonc file");
return undefined;
}
if ("durable_objects" in existingWranglerConfig) {
delete existingWranglerConfig["durable_objects"];
} else {
// If the durable_objects key is not present, we don't need to patch the wrangler config
return undefined;
}

// We need to create the .open-next folder if it doesn't exist
await fs.mkdir(`${process.cwd()}/.open-next`, { recursive: true });
// We copy the .dev.vars file to .open-next/.dev.vars
try {
await fs.cp(`${process.cwd()}/.dev.vars`, `${process.cwd()}/.open-next/.dev.vars`);
} catch {
// There is no .dev.vars file
}
await fs.writeFile(patchedWranglerConfigPath, JSON.stringify(existingWranglerConfig));
} catch {
console.error("If you use one of the DO overrides, you need to have a wrangler.jsonc file");
return undefined;
}
return existingWranglerConfig ? patchedWranglerConfigPath : undefined;
}

/**
* Gets a cloudflare context object from wrangler
*
Expand All @@ -291,11 +329,13 @@ async function getCloudflareContextFromWrangler<
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
Context = ExecutionContext,
>(): Promise<CloudflareContext<CfProperties, Context>> {
const configPath = await patchWranglerConfig();
// 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,
configPath,
});
return {
env,
Expand Down