Skip to content

Commit 75fc340

Browse files
inline setting of context and fix wrong types
1 parent 477e2a1 commit 75fc340

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ export function getCloudflareContext<
9797
}
9898

9999
if (options.async) {
100-
return getAndSetCloudflareContextInNodejs();
100+
return getCloudflareContextFromWrangler<CfProperties, Context>().then((context) => {
101+
addCloudflareContextToNodejsGlobal(context);
102+
return context;
103+
});
101104
}
102105

103106
// the cloudflare context is initialized by the worker and is always present in production/preview
@@ -159,7 +162,10 @@ function shouldContextInitializationRun(): boolean {
159162
*
160163
* @param cloudflareContext the cloudflare context to add to the node.sj global scope
161164
*/
162-
function addCloudflareContextToNodejsGlobal(cloudflareContext: CloudflareContext<CfProperties, Context>) {
165+
function addCloudflareContextToNodejsGlobal<
166+
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
167+
Context = ExecutionContext,
168+
>(cloudflareContext: CloudflareContext<CfProperties, Context>) {
163169
const global = globalThis as InternalGlobalThis<CfProperties, Context>;
164170
global[cloudflareContextSymbol] = cloudflareContext;
165171
}
@@ -219,19 +225,3 @@ async function getCloudflareContextFromWrangler<
219225
ctx: ctx as Context,
220226
};
221227
}
222-
223-
/**
224-
* Gets a local proxy version of the cloudflare context (created using `getPlatformProxy`) when running
225-
* in a Node.js process (so under `next dev` or for ssg under `next build`), is also sets this value on the
226-
* globalThis so that it can be accessed later.
227-
*
228-
* @returns the local proxy version of the cloudflare context
229-
*/
230-
async function getAndSetCloudflareContextInNodejs<
231-
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
232-
Context = ExecutionContext,
233-
>(): Promise<CloudflareContext<CfProperties, Context>> {
234-
const context = await getCloudflareContextFromWrangler();
235-
addCloudflareContextToNodejsGlobal(context);
236-
return context as unknown as CloudflareContext<CfProperties, Context>;
237-
}

0 commit comments

Comments
 (0)