Skip to content

DynamicWorkerLoader

Source: src/Cloudflare/Workers/DynamicWorkerLoader.ts

Load and run ephemeral Workers at runtime from inline JavaScript modules.

DynamicWorkerLoader registers a worker_loader binding on the parent Worker at deploy time. At runtime you call .load() with inline module source code and get back a fully typed Worker instance you can fetch or call RPC methods on. Each loaded Worker runs in its own isolate with full sandboxing.

This is useful for evaluating user-provided code, running untrusted plugins, or dynamically generating Workers from templates.

Yield Cloudflare.DynamicWorkerLoader in your Worker’s init phase to register the binding. The string argument becomes the binding name on the deployed Worker.

// init
const loader = yield* Cloudflare.DynamicWorkerLoader("Loader");

Call loader.load() with a compatibility date, a main module name, and a map of module names to source code strings. The returned instance exposes .fetch() for HTTP and RPC methods for named entrypoints.

const worker = loader.load({
compatibilityDate: "2026-01-28",
mainModule: "worker.js",
modules: {
"worker.js": `export default {
async fetch(request) {
return new Response("Hello from dynamic worker!");
}
}`,
},
});
const response = yield* worker.fetch(
HttpClientRequest.get("https://worker/"),
);

Set globalOutbound to null to block all outbound network access from the dynamic Worker, or pass an RPC stub to intercept and proxy outbound requests.

const worker = loader.load({
compatibilityDate: "2026-01-28",
mainModule: "worker.js",
modules: {
"worker.js": `export default {
async fetch(req) {
// fetch() calls from here will fail
return new Response("sandboxed");
}
}`,
},
globalOutbound: null,
});

If the dynamic Worker exports named entrypoints, use .getEntrypoint(name) to get a typed stub for calling its methods.

const worker = loader.load({ ... });
const api = worker.getEntrypoint<{ greet: (name: string) => Effect.Effect<string> }>("api");
const greeting = yield* api.greet("world");