Skip to content

Commit

Permalink
enable getCloudflareContext to also work with next dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Sep 25, 2024
1 parent af15fd1 commit 80773ad
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
7 changes: 6 additions & 1 deletion examples/api/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
webpack: (config) => {
config.externals = ["wrangler"];
return config;
},
};

export default nextConfig;
6 changes: 3 additions & 3 deletions examples/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"dependencies": {
"next": "catalog:",
"react": "catalog:",
"react-dom": "catalog:"
"react-dom": "catalog:",
"wrangler": "catalog:"
},
"devDependencies": {
"@opennextjs/cloudflare": "workspace:*",
"@playwright/test": "catalog:",
"@types/node": "catalog:",
"wrangler": "catalog:"
"@types/node": "catalog:"
}
}
3 changes: 3 additions & 0 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
},
"dependencies": {
"ts-morph": "catalog:"
},
"peerDependencies": {
"wrangler": "catalog:"
}
}
45 changes: 37 additions & 8 deletions packages/cloudflare/src/api/get-cloudflare-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,46 @@ export async function getCloudflareContext<
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
Context = ExecutionContext,
>(): Promise<CloudflareContext<CfProperties, Context>> {
const cloudflareContext = (
globalThis as unknown as {
[cloudflareContextSymbol]: CloudflareContext<CfProperties, Context> | undefined;
}
)[cloudflareContextSymbol];
const global = globalThis as unknown as {
[cloudflareContextSymbol]: CloudflareContext<CfProperties, Context> | undefined;
};

const cloudflareContext = global[cloudflareContextSymbol];

if (!cloudflareContext) {
// TODO: cloudflareContext should always be present in production/preview, if not it means that this
// is running under `next dev`, in this case use `getPlatformProxy` to return local proxies
throw new Error("Cloudflare context is not defined!");
// the cloudflare context is initialized by the worker and is always present in production/preview,
// so, it not being present means that the application is running under `next dev`
return getCloudflareContextInNextDev();
}

return cloudflareContext;
}

const cloudflareContextInNextDevSymbol = Symbol.for("__next-dev/cloudflare-context__");

/**
* Gets a local proxy version of the cloudflare context (created using `getPlatformProxy`) when
* running in the standard next dev server (via `next dev`)
*
* @returns the local proxy version of the cloudflare context
*/
async function getCloudflareContextInNextDev<
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
Context = ExecutionContext,
>(): Promise<CloudflareContext<CfProperties, Context>> {
const global = globalThis as unknown as {
[cloudflareContextInNextDevSymbol]: CloudflareContext<CfProperties, Context> | undefined;
};

if (!global[cloudflareContextInNextDevSymbol]) {
const { getPlatformProxy } = await import("wrangler");
const { env, cf, ctx } = await getPlatformProxy();
global[cloudflareContextInNextDevSymbol] = {
env,
cf: cf as unknown as CfProperties,
ctx: ctx as Context,
};
}

return global[cloudflareContextInNextDevSymbol]!;
}
2 changes: 2 additions & 0 deletions packages/cloudflare/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const cliConfig = defineConfig({
format: ["esm"],
platform: "node",
external: ["esbuild"],
clean: true,
onSuccess: async () => {
await cp(`${__dirname}/src/cli/templates`, `${__dirname}/dist/cli/templates`, {
recursive: true,
Expand All @@ -22,6 +23,7 @@ const apiConfig = defineConfig({
format: ["esm"],
platform: "node",
external: ["server-only"],
clean: true,
});

export default [cliConfig, apiConfig];
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 80773ad

Please sign in to comment.