Worker struct defines the code, compatibility settings, bindings, and storage configuration for a JavaScript or WebAssembly worker.
Code definition
ES modules that compose the worker. The first module is the main module which exports event handlers.
Alternative to modules: a single script using global
addEventListener() to register event handlers (service worker syntax).Use Cap’n Proto’s embed directive to read from an external file:Inherit configuration from another worker by service name. Creates a clone with modifiable bindings and
globalOutbound, but compatibility settings cannot be changed.Module types
Name or path used to import the module.
ES module file with imports and exports.
CommonJS module using
require().Raw text blob. Importing produces a string.
Raw data blob. Importing produces an ArrayBuffer.
Compiled binary WebAssembly module. Importing produces a
WebAssembly.Module object.JSON data. Importing produces the parsed result.
Python module. All bundles containing this type are converted into a JS/WASM worker bundle prior to execution.
Python package required by the bundle. Must be supported by Pyodide. Only the module name matters; the field value is ignored.
Compatibility
Compatibility date in YYYY-MM-DD format. Required unless inheriting from another worker.See Cloudflare Workers compatibility dates for details.
Optional compatibility flags to enable specific behaviors.
Bindings
Bindings give the worker access to external resources and configuration settings.For ES modules syntax, bindings are delivered via the
env object. For service workers syntax, each binding appears as a global variable.Outbound configuration
Where the global
fetch() function sends requests. Defaults to the “internet” service.Where Cache API requests (
caches.default, caches.open()) are sent.Durable Objects
List of Durable Object namespaces in this worker.
Exported class name implementing the Durable Object. Can be changed without breaking compatibility if
uniqueKey stays the same.Unique, stable ID for this namespace (e.g., a GUID). Used to derive object IDs cryptographically.DO NOT LOSE this key, or stored data may become inaccessible.
Marks instances as ephemeral with no durable storage. The
state.storage API will not be present.Keep Durable Objects pinned to memory forever. Only supported in workerd; not available in production.
Allow Durable Objects in this namespace to use the
storage.sql API for SQL queries.Specifies where this worker’s Durable Objects are stored.Options:
none: No Durable Objects (default)inMemory: Store in memory only (for testing)localDisk: Store in a directory on disk (experimental)
Additional text hashed with
uniqueKey for inherited workers. Each derived worker must specify a unique modifier.DO NOT LOSE this value.