Skip to content

Commit 7fda6ca

Browse files
fixup! enable getCloudflareContext to work in middlewares via a new enableEdgeDevGetCloudflareContext utility
use `process.env.NEXT_RUNTIME` to detect the edge runtime instead of try-catching
1 parent 59419ca commit 7fda6ca

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,27 @@ async function getCloudflareContextInNextDev<
6767
};
6868

6969
if (!global[cloudflareContextInNextDevSymbol]) {
70-
try {
71-
const cloudflareContext = await getCloudflareContextFromWrangler<CfProperties, Context>();
72-
global[cloudflareContextInNextDevSymbol] = cloudflareContext;
73-
} catch (e: unknown) {
74-
if (e instanceof Error && e.message.includes("A dynamic import callback was not specified")) {
75-
const getCloudflareContextFunctionName = getCloudflareContext.name;
76-
const enablingFunctionName = enableEdgeDevGetCloudflareContext.name;
77-
throw new Error(
78-
`\n\n\`${getCloudflareContextFunctionName}\` has been invoked during development inside the edge runtime ` +
79-
"this is not enabled, to enable such use of the function you need to import and call the " +
80-
`\`${enablingFunctionName}\` function inside your Next.js config file\n\n"` +
81-
"Example: \n ```\n // next.config.mjs\n\n" +
82-
` import { ${enablingFunctionName} } from "@opennextjs/cloudflare";\n\n` +
83-
` ${enablingFunctionName}();\n\n` +
84-
" /** @type {import('next').NextConfig} */\n" +
85-
" const nextConfig = {};\n" +
86-
" export default nextConfig;\n" +
87-
" ```\n" +
88-
"\n(note: currently middlewares in Next.js are always run using the edge runtime)\n\n"
89-
);
90-
} else {
91-
throw e;
92-
}
70+
const inEdgeRuntime = process.env.NEXT_RUNTIME === "edge";
71+
if (inEdgeRuntime) {
72+
const getCloudflareContextFunctionName = getCloudflareContext.name;
73+
const enablingFunctionName = enableEdgeDevGetCloudflareContext.name;
74+
throw new Error(
75+
`\n\n\`${getCloudflareContextFunctionName}\` has been invoked during development inside the edge runtime ` +
76+
"this is not enabled, to enable such use of the function you need to import and call the " +
77+
`\`${enablingFunctionName}\` function inside your Next.js config file\n\n"` +
78+
"Example: \n ```\n // next.config.mjs\n\n" +
79+
` import { ${enablingFunctionName} } from "@opennextjs/cloudflare";\n\n` +
80+
` ${enablingFunctionName}();\n\n` +
81+
" /** @type {import('next').NextConfig} */\n" +
82+
" const nextConfig = {};\n" +
83+
" export default nextConfig;\n" +
84+
" ```\n" +
85+
"\n(note: currently middlewares in Next.js are always run using the edge runtime)\n\n"
86+
);
9387
}
88+
89+
const cloudflareContext = await getCloudflareContextFromWrangler<CfProperties, Context>();
90+
global[cloudflareContextInNextDevSymbol] = cloudflareContext;
9491
}
9592

9693
return global[cloudflareContextInNextDevSymbol]!;

0 commit comments

Comments
 (0)