Skip to content

Commit 59677f6

Browse files
committed
fixup! pass run_worker_first via globalThis
1 parent ae48f4d commit 59677f6

File tree

5 files changed

+12
-18
lines changed

5 files changed

+12
-18
lines changed

packages/cloudflare/src/api/overrides/asset-resolver/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const resolver: AssetResolver = {
1818
async maybeGetAssetResult(event: InternalEvent) {
1919
const { ASSETS } = getCloudflareContext().env;
2020

21-
if (!ASSETS || !isUserWorkerFirst(__CF_ASSETS_RUN_WORKER_FIRST__, event.rawPath)) {
21+
if (!ASSETS || !isUserWorkerFirst(globalThis.__ASSETS_RUN_WORKER_FIRST__, event.rawPath)) {
2222
// Only handle assets when the user worker runs first for the path
2323
return undefined;
2424
}
@@ -94,11 +94,3 @@ export function isUserWorkerFirst(runWorkerFirst: boolean | string[] | undefined
9494
}
9595

9696
export default resolver;
97-
98-
/* eslint-disable no-var */
99-
declare global {
100-
// The configuration of `run_worker_first` for the assets.
101-
// Replaced at built time.
102-
var __CF_ASSETS_RUN_WORKER_FIRST__: boolean | string[] | undefined;
103-
}
104-
/* eslint-enable no-var */

packages/cloudflare/src/cli/build/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function build(
6666
compileEnvFiles(options);
6767

6868
// Compile workerd init
69-
compileInit(options);
69+
compileInit(options, wranglerConfig);
7070

7171
// Compile image helpers
7272
compileImages(options);
@@ -88,7 +88,7 @@ export async function build(
8888

8989
await compileDurableObjects(options);
9090

91-
await bundleServer(options, wranglerConfig);
91+
await bundleServer(options);
9292

9393
if (!projectOpts.skipWranglerConfigCheck) {
9494
await createWranglerConfigIfNotExistent(projectOpts);

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { fileURLToPath } from "node:url";
66
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
77
import { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js";
88
import { build, type Plugin } from "esbuild";
9-
import type { Unstable_Config } from "wrangler";
109

1110
import { getOpenNextConfig } from "../../api/config.js";
1211
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js";
@@ -45,7 +44,7 @@ const optionalDependencies = [
4544
/**
4645
* Bundle the Open Next server.
4746
*/
48-
export async function bundleServer(buildOpts: BuildOptions, wranglerConfig: Unstable_Config): Promise<void> {
47+
export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
4948
copyPackageCliFiles(packageDistDir, buildOpts);
5049

5150
const { appPath, outputDir, monorepoRoot, debug } = buildOpts;
@@ -147,8 +146,6 @@ export async function bundleServer(buildOpts: BuildOptions, wranglerConfig: Unst
147146
"process.env.TURBOPACK": "false",
148147
// This define should be safe to use for Next 14.2+, earlier versions (13.5 and less) will cause trouble
149148
"process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
150-
// Pass `assets.run_worker_first` to the asset resolver
151-
__CF_ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
152149
},
153150
banner: {
154151
// We need to import them here, assigning them to `globalThis` does not work because node:timers use `globalThis` and thus create an infinite loop

packages/cloudflare/src/cli/build/open-next/compile-init.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { fileURLToPath } from "node:url";
44
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
55
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
66
import { build } from "esbuild";
7+
import type { Unstable_Config } from "wrangler";
78

89
/**
910
* Compiles the initialization code for the workerd runtime
1011
*/
11-
export async function compileInit(options: BuildOptions) {
12+
export async function compileInit(options: BuildOptions, wranglerConfig: Unstable_Config) {
1213
const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url)));
1314
const templatesDir = path.join(currentDir, "../../templates");
1415
const initPath = path.join(templatesDir, "init.js");
@@ -27,6 +28,7 @@ export async function compileInit(options: BuildOptions) {
2728
define: {
2829
__BUILD_TIMESTAMP_MS__: JSON.stringify(Date.now()),
2930
__NEXT_BASE_PATH__: JSON.stringify(basePath),
31+
__ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
3032
},
3133
});
3234
}

packages/cloudflare/src/cli/templates/init.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ function initRuntime() {
9494

9595
Object.assign(globalThis, {
9696
Request: CustomRequest,
97-
__BUILD_TIMESTAMP_MS__: __BUILD_TIMESTAMP_MS__,
98-
__NEXT_BASE_PATH__: __NEXT_BASE_PATH__,
97+
__BUILD_TIMESTAMP_MS__,
98+
__NEXT_BASE_PATH__,
99+
__ASSETS_RUN_WORKER_FIRST__,
99100
// The external middleware will use the convertTo function of the `edge` converter
100101
// by default it will try to fetch the request, but since we are running everything in the same worker
101102
// we need to use the request as is.
@@ -146,5 +147,7 @@ declare global {
146147
var __BUILD_TIMESTAMP_MS__: number;
147148
// Next basePath
148149
var __NEXT_BASE_PATH__: string;
150+
// Value of `run_worker_first` for the asset binding
151+
var __ASSETS_RUN_WORKER_FIRST__: boolean | string[] | undefined;
149152
}
150153
/* eslint-enable no-var */

0 commit comments

Comments
 (0)