Skip to content

Commit abd7f3c

Browse files
add if-else statement around asyncMode
1 parent 213fdba commit abd7f3c

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,28 @@ export function getCloudflareContext<
9191
// source: https://github.com/vercel/next.js/blob/4e394608423/packages/next/src/export/worker.ts#L55-L57)
9292
const inSSG = global.__NEXT_DATA__?.nextExport === true;
9393

94-
if ((inNodejsRuntime || inSSG) && asyncMode) {
95-
// we're in a node.js process and also in "async mode" so we can use wrangler to asynchronously get the context
96-
return getCloudflareContextFromWrangler<CfProperties, Context>().then((context) => {
97-
addCloudflareContextToNodejsGlobal(context);
98-
return context;
99-
});
100-
}
101-
102-
// The sync mode of `getCloudflareContext`, relies on the context being set on the global state
103-
// by either the worker entrypoint (in prod) or by `initOpenNextCloudflareForDev` (in dev), neither
104-
// can work during SSG since for SSG Next.js creates (jest) workers that don't get access to the
105-
// normal global state so we throw with a helpful error message.
106-
if (inSSG) {
107-
throw new Error(
108-
`\n\nERROR: \`getCloudflareContext\` has been called in a static route,` +
109-
` that is not allowed, this can be solved in different ways:\n\n` +
110-
` - call \`getCloudflareContext({async: true})\` to use the \`async\` mode\n` +
111-
` - avoid calling \`getCloudflareContext\` in the route\n` +
112-
` - make the route non static\n`
113-
);
94+
if (asyncMode) {
95+
if (inNodejsRuntime || inSSG) {
96+
// we're in a node.js process and also in "async mode" so we can use wrangler to asynchronously get the context
97+
return getCloudflareContextFromWrangler<CfProperties, Context>().then((context) => {
98+
addCloudflareContextToNodejsGlobal(context);
99+
return context;
100+
});
101+
}
102+
} else {
103+
// The sync mode of `getCloudflareContext`, relies on the context being set on the global state
104+
// by either the worker entrypoint (in prod) or by `initOpenNextCloudflareForDev` (in dev), neither
105+
// can work during SSG since for SSG Next.js creates (jest) workers that don't get access to the
106+
// normal global state so we throw with a helpful error message.
107+
if (inSSG) {
108+
throw new Error(
109+
`\n\nERROR: \`getCloudflareContext\` has been called in a static route,` +
110+
` that is not allowed, this can be solved in different ways:\n\n` +
111+
` - call \`getCloudflareContext({async: true})\` to use the \`async\` mode\n` +
112+
` - avoid calling \`getCloudflareContext\` in the route\n` +
113+
` - make the route non static\n`
114+
);
115+
}
114116
}
115117

116118
// The cloudflare context is initialized by the worker so it is always available.

0 commit comments

Comments
 (0)