Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 4d68d67

Browse files
authored
Add __STATIC_CONTENT_MANIFEST module allowing module mode sites (#398)
Wrangler injects a text `__STATIC_CONTENT_MANIFEST` module in modules mode for use with `@cloudflare/kv-asset-handler`. See https://github.com/cloudflare/kv-asset-handler#es-modules.
1 parent 610a5ef commit 4d68d67

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

packages/tre/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
Plugins,
2424
SERVICE_ENTRY,
2525
SOCKET_ENTRY,
26+
maybeGetSitesManifestModule,
2627
normaliseDurableObject,
2728
} from "./plugins";
2829
import { HEADER_CUSTOM_SERVICE, getUserServiceName } from "./plugins/core";
@@ -33,6 +34,7 @@ import {
3334
Service,
3435
Socket,
3536
Worker_Binding,
37+
Worker_Module,
3638
getSupportedRuntime,
3739
serializeConfig,
3840
} from "./runtime";
@@ -403,12 +405,20 @@ export class Miniflare {
403405

404406
for (let i = 0; i < allWorkerOpts.length; i++) {
405407
const workerOpts = allWorkerOpts[i];
408+
406409
// Collect all bindings from this worker
407410
const workerBindings: Worker_Binding[] = [];
411+
const additionalModules: Worker_Module[] = [];
408412
for (const [key, plugin] of PLUGIN_ENTRIES) {
409413
const pluginBindings = await plugin.getBindings(workerOpts[key]);
410414
if (pluginBindings !== undefined) {
411415
workerBindings.push(...pluginBindings);
416+
417+
if (key === "kv") {
418+
// Add "__STATIC_CONTENT_MANIFEST" module if sites enabled
419+
const module = maybeGetSitesManifestModule(pluginBindings);
420+
if (module !== undefined) additionalModules.push(module);
421+
}
412422
}
413423
}
414424

@@ -421,6 +431,7 @@ export class Miniflare {
421431
workerBindings,
422432
workerIndex: i,
423433
durableObjectClassNames,
434+
additionalModules,
424435
});
425436
if (pluginServices !== undefined) {
426437
for (const service of pluginServices) {

packages/tre/src/plugins/core/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export const CORE_PLUGIN: Plugin<
209209
workerIndex,
210210
sharedOptions,
211211
durableObjectClassNames,
212+
additionalModules,
212213
}) {
213214
// Define core/shared services.
214215
// Services get de-duped by name, so only the first worker's
@@ -232,6 +233,11 @@ export const CORE_PLUGIN: Plugin<
232233
// Define regular user worker if script is set
233234
const workerScript = getWorkerScript(options);
234235
if (workerScript !== undefined) {
236+
// Add additional modules (e.g. "__STATIC_CONTENT_MANIFEST") if any
237+
if ("modules" in workerScript) {
238+
workerScript.modules.push(...additionalModules);
239+
}
240+
235241
const name = getUserServiceName(options.name);
236242
const classNames = durableObjectClassNames.get(name) ?? [];
237243
const compatibilityDate = validateCompatibilityDate(

packages/tre/src/plugins/kv/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ export const KV_PLUGIN: Plugin<
8888
};
8989

9090
export * from "./gateway";
91+
export { maybeGetSitesManifestModule } from "./sites";
9192
export { KV_PLUGIN_NAME };

packages/tre/src/plugins/kv/sites.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
22
import { pathToFileURL } from "url";
3-
import { Service, Worker_Binding } from "../../runtime";
3+
import { Service, Worker_Binding, Worker_Module } from "../../runtime";
44
import {
55
MatcherRegExps,
66
deserialiseRegExps,
@@ -171,6 +171,17 @@ export async function getSitesBindings(
171171
];
172172
}
173173

174+
export function maybeGetSitesManifestModule(
175+
bindings: Worker_Binding[]
176+
): Worker_Module | undefined {
177+
for (const binding of bindings) {
178+
if (binding.name === BINDING_JSON_SITE_MANIFEST) {
179+
assert("json" in binding && binding.json !== undefined);
180+
return { name: BINDING_JSON_SITE_MANIFEST, text: binding.json };
181+
}
182+
}
183+
}
184+
174185
export function getSitesService(options: SitesOptions): Service {
175186
// `siteRegExps` should've been set in `getSitesBindings()`, and `options`
176187
// should be the same object reference as before.

packages/tre/src/plugins/shared/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { Service, Worker_Binding } from "../../runtime";
2+
import { Service, Worker_Binding, Worker_Module } from "../../runtime";
33
import { Awaitable, OptionalZodTypeOf } from "../../shared";
44
import { GatewayConstructor } from "./gateway";
55
import { RouterConstructor } from "./router";
@@ -16,6 +16,7 @@ export interface PluginServicesOptions<
1616
workerBindings: Worker_Binding[];
1717
workerIndex: number;
1818
durableObjectClassNames: DurableObjectClassNames;
19+
additionalModules: Worker_Module[];
1920
}
2021

2122
export interface PluginBase<

0 commit comments

Comments
 (0)