Skip to content

Commit 347472b

Browse files
Style and structure fixes across 9 files
Co-authored-by: elithrar <elithrar@users.noreply.github.com>
1 parent c2c8e5f commit 347472b

9 files changed

Lines changed: 89 additions & 91 deletions

File tree

src/content/docs/dynamic-workers/api-reference.mdx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,65 @@ products:
1010

1111
import { Type, MetaInfo } from "~/components";
1212

13-
This page describes the Worker Loader binding API, assuming you have [configured such a binding](/dynamic-workers/getting-started/#configure-worker-loader) as `env.LOADER`.
13+
This page describes the Worker Loader binding API, assuming you have [configured a binding](/dynamic-workers/getting-started/#configure-worker-loader) as `env.LOADER`.
1414

15-
### `load`
15+
## `load`
1616

1717
<code>env.LOADER.load(code <Type text="WorkerCode" />) <Type text="WorkerStub" /></code>
1818

1919
Loads a Worker from the provided `WorkerCode` and returns a `WorkerStub` which may be used to invoke the Worker.
2020

21-
Unlike `get()`, `load()` does not cache by ID. Each call creates a fresh Worker.
21+
Unlike `get()`, `load()` does not cache by ID. Each call creates a new Worker.
2222

23-
Use `load()` when the code is always new, such as for one-off AI-generated tool calls.
23+
Use `load()` when the code is always new, such as for one-time AI-generated tool calls.
2424

25-
### `get`
25+
## `get`
2626

2727
<code>env.LOADER.get(id <Type text="string" />, getCodeCallback <Type text="() => Promise<WorkerCode>" />): <Type text="WorkerStub" /></code>
2828

2929
Loads a Worker with the given ID, returning a `WorkerStub` which may be used to invoke the Worker.
3030

31-
As a convenience, the loader implements caching of isolates. When a new ID is seen the first time, a new isolate is loaded. But, the isolate may be kept warm in memory for a while. If later invocations of the loader request the same ID, the existing isolate may be returned again, rather than create a new one. But there is no guarantee: a later call with the same ID may instead start a new isolate from scratch.
31+
The loader caches isolates by ID. When the loader sees an ID for the first time, it creates a new isolate. The isolate may stay warm in memory for subsequent requests. If a later call uses the same ID, the runtime may return the existing isolate instead of creating a new one. However, there is no guarantee &mdash; a later call with the same ID may start a new isolate from scratch.
3232

33-
Whenever the system determines it needs to start a new isolate, and it does not already have a copy of the code cached, it will invoke `codeCallback` to get the Worker's code. This is an async callback, so the application can load the code from remote storage if desired. The callback returns a `WorkerCode` object (described below).
33+
When the runtime needs to start a new isolate and does not have a cached copy of the code, it invokes `codeCallback` to get the Worker code. This is an async callback, so the application can load the code from remote storage if needed. The callback returns a `WorkerCode` object (described below).
3434

35-
Because of the caching, you should ensure that the callback always returns exactly the same content, when called for the same ID. If anything about the content changes, you must use a new ID. But if the content hasn't changed, it's best to reuse the same ID in order to take advantage of caching. If the `WorkerCode` is different every time, you can pass a random ID.
35+
Because of caching, ensure that the callback always returns exactly the same content for the same ID. If anything about the content changes, you must use a new ID. If the content has not changed, reuse the same ID to take advantage of caching. If the `WorkerCode` is different every time, pass a random ID.
3636

37-
You could, for example, use IDs of the form `<worker-name>:<version-number>`, where the version number increments every time the code changes. Or, you could compute IDs based on a hash of the code and config, so that any change results in a new ID.
37+
For example, use IDs of the form `<worker-name>:<version-number>`, where the version number increments every time the code changes. Alternatively, compute IDs based on a hash of the code and config so that any change produces a new ID.
3838

39-
`get()` returns a `WorkerStub`, which can be used to send requests to the loaded Worker. Note that the stub is returned synchronously&mdash;you do not have to await it. If the Worker is not loaded yet, requests made to the stub will wait for the Worker to load before being delivered. If loading fails, the request will throw an exception.
39+
`get()` returns a `WorkerStub`, which you can use to send requests to the loaded Worker. The stub is returned synchronously &mdash; you do not have to await it. If the Worker is not loaded yet, requests made to the stub wait for the Worker to load before being delivered. If loading fails, the request throws an exception.
4040

41-
It is never guaranteed that two requests will go to the same isolate. Even if you use the same `WorkerStub` to make multiple requests, they could execute in different isolates. The callback passed to `loader.get()` could be called any number of times (although it is unusual for it to be called more than once).
41+
Two requests are never guaranteed to reach the same isolate. Even if you use the same `WorkerStub` to make multiple requests, they could execute in different isolates. The callback passed to `loader.get()` could run any number of times, although it is unusual for it to run more than once.
4242

43-
### `WorkerCode`
43+
## `WorkerCode`
4444

4545
This is the structure returned by `getCodeCallback` to represent a worker.
4646

47-
#### <code>compatibilityDate <Type text="string" /></code>
47+
### <code>compatibilityDate <Type text="string" /></code>
4848

4949
The [compatibility date](/workers/configuration/compatibility-dates/) for the Worker. This has the same meaning as the `compatibility_date` setting in a Wrangler config file.
5050

51-
#### <code>compatibilityFlags <Type text="string[]" /> <MetaInfo text='Optional' /></code>
51+
### <code>compatibilityFlags <Type text="string[]" /> <MetaInfo text='Optional' /></code>
5252

5353
An optional list of [compatibility flags](/workers/configuration/compatibility-flags) augmenting the compatibility date. This has the same meaning as the `compatibility_flags` setting in a Wrangler config file.
5454

55-
#### <code>allowExperimental <Type text="boolean" /> <MetaInfo text='Optional' /></code>
55+
### <code>allowExperimental <Type text="boolean" /> <MetaInfo text='Optional' /></code>
5656

57-
If true, then experimental compatibility flags will be permitted in `compatibilityFlags`. In order to set this, the worker calling the loader must itself have the compatibility flag `"experimental"` set. Experimental flags cannot be enabled in production.
57+
If true, experimental compatibility flags are permitted in `compatibilityFlags`. To set this, the Worker calling the loader must itself have the `"experimental"` compatibility flag set. Experimental flags cannot be used in production.
5858

59-
#### <code>mainModule <Type text="string" /></code>
59+
### <code>mainModule <Type text="string" /></code>
6060

6161
The name of the Worker's main module. This must be one of the modules listed in `modules`.
6262

63-
#### <code>modules <Type text="Record<string, string | Module>"/></code>
63+
### <code>modules <Type text="Record&lt;string, string | Module&gt;"/></code>
6464

65-
A dictionary object mapping module names to their string contents. If the module content is a plain string, then the module name must have a file extension indicating its type: either `.js` or `.py`.
65+
A dictionary object mapping module names to their string contents. If the module content is a plain string, the module name must have a file extension indicating its type: either `.js` or `.py`.
6666

67-
A module's content can also be specified as an object, in order to specify its type independent from the name. The allowed objects are:
67+
You can also specify a module's content as an object to set its type independent of the name. The allowed objects are:
6868

6969
- `{js: string}`: A JavaScript module, using ES modules syntax for imports and exports.
7070
- `{cjs: string}`: A CommonJS module, using `require()` syntax for imports.
71-
- `{py: string}`: A [Python module](/workers/languages/python/). See warning below.
71+
- `{py: string}`: A [Python module](/workers/languages/python/). Refer to the warning below.
7272
- `{text: string}`: An importable string value.
7373
- `{data: ArrayBuffer}`: An importable `ArrayBuffer` value.
7474
- `{json: object}`: An importable object. The value must be JSON-serializable. However, note that value is provided as a parsed object, and is delivered as a parsed object; neither side actually sees the JSON serialization.
@@ -79,17 +79,17 @@ While Dynamic Workers support Python, Python Workers are currently much slower t
7979

8080
:::
8181

82-
#### <code>globalOutbound <Type text="ServiceStub | null" /> <MetaInfo text='Optional' /></code>
82+
### <code>globalOutbound <Type text="ServiceStub | null" /> <MetaInfo text='Optional' /></code>
8383

8484
Controls whether the dynamic Worker has access to the network. The global `fetch()` and `connect()` functions (for making HTTP requests and TCP connections, respectively) can be blocked or redirected to isolate the Worker.
8585

86-
If `globalOutbound` is not specified, the default is to inherit the parent's network access, which usually means the dynamic Worker will have full access to the public Internet.
86+
If `globalOutbound` is not specified, the default is to inherit the parent's network access, which usually means the dynamic Worker has full access to the public internet.
8787

88-
If `globalOutbound` is `null`, then the dynamic Worker will be totally cut off from the network. Both `fetch()` and `connect()` will throw exceptions.
88+
If `globalOutbound` is `null`, the dynamic Worker is fully cut off from the network. Both `fetch()` and `connect()` throw exceptions.
8989

90-
`globalOutbound` can also be set to any service binding, including service bindings in the parent worker's `env` as well as [loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
90+
You can also set `globalOutbound` to any service binding, including service bindings in the parent Worker's `env` and [loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
9191

92-
Using `ctx.exports` is particularly useful as it allows you to customize the binding further for the specific sandbox, by setting the value of `ctx.props` that should be passed back to it. The `props` can contain information to identify the specific dynamic Worker that made the request.
92+
Using `ctx.exports` is particularly useful because it lets you customize the binding for a specific sandbox by setting the value of `ctx.props` that is passed back to it. The `props` can contain information to identify the specific dynamic Worker that made the request.
9393

9494
For example:
9595

@@ -120,13 +120,13 @@ export default {
120120
};
121121
```
122122

123-
#### <code>env <Type text="object" /></code>
123+
### <code>env <Type text="object" /></code>
124124

125125
The environment object to provide to the dynamic Worker.
126126

127127
Using this, you can provide custom bindings to the Worker.
128128

129-
`env` is serialized and transferred into the dynamic Worker, where it is used directly as the value of `env` there. It may contain:
129+
`env` is serialized and transferred into the dynamic Worker, where it becomes the value of `env`. It may contain:
130130

131131
- [Structured clonable types](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
132132
- [Service Bindings](/workers/runtime-apis/bindings/service-bindings), including [loopback bindings from `ctx.exports`](/workers/runtime-apis/context/#exports).
@@ -164,9 +164,9 @@ export default {
164164
};
165165
```
166166

167-
#### <code>tails <Type text="ServiceStub[]" /> <MetaInfo text='Optional' /></code>
167+
### <code>tails <Type text="ServiceStub[]" /> <MetaInfo text='Optional' /></code>
168168

169-
You may specify one or more [Tail Workers](/workers/observability/logs/tail-workers/) which will observe console logs, errors, and other details about the dynamically-loaded worker's execution. A tail event will be delivered to the Tail Worker upon completion of a request to the dynamically-loaded Worker. As always, you can implement the Tail Worker as an alternative entrypoint in your parent Worker, referring to it using `ctx.exports`:
169+
Specify one or more [Tail Workers](/workers/observability/logs/tail-workers/) to observe console logs, errors, and other details about the dynamically-loaded Worker's execution. A tail event is delivered to the Tail Worker when a request to the dynamically-loaded Worker completes. You can implement the Tail Worker as an alternative entrypoint in your parent Worker, referencing it with `ctx.exports`:
170170

171171
```js
172172
import { WorkerEntrypoint } from "cloudflare:workers";

src/content/docs/dynamic-workers/examples/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Examples
3-
description: See common Dynamic Workers patterns for code execution and sandboxing.
3+
description: Common Dynamic Workers patterns for code execution and sandboxing.
44
pcx_content_type: navigation
55
sidebar:
66
order: 4

src/content/docs/dynamic-workers/getting-started.mdx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,31 @@ Dynamic Workers support two loading modes:
1919

2020
`load()` is best for one-time code execution, for example when using [Codemode](/agents/api-reference/codemode/). `get(id, callback)` is better when the same code will receive subsequent requests, for example when you are building applications.
2121

22-
### Try it out
22+
## Try it out
2323

24-
#### Dynamic Workers Starter
24+
### Dynamic Workers Starter
2525

2626
[![Deploy to Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers)
2727

2828
Use this "hello world" [starter](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers) to get a Worker deployed that can load and execute Dynamic Workers.
2929

30-
#### Dynamic Workers Playground
30+
### Dynamic Workers Playground
3131

3232
[![Deploy to Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground)
3333

3434
You can also deploy the [Dynamic Workers Playground](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground), where you can write or import code, bundle it at runtime with `@cloudflare/worker-bundler`, execute it through a Dynamic Worker, and see real-time responses and execution logs.
3535

3636
## Configure Worker Loader
3737

38-
In order for a Worker to be able to create Dynamic Workers, it needs a Worker Loader binding. Unlike most Workers bindings, this binding doesn't point at any external resource in particular; it simply provides access to the Worker Loader API.
38+
For a Worker to create Dynamic Workers, it needs a Worker Loader binding. Unlike most Workers bindings, this binding does not point at any external resource. It provides access to the Worker Loader API.
3939

40-
Configure it like so, in your Worker's `wrangler.jsonc`:
40+
Add the following to your Worker's Wrangler configuration:
4141

4242
<WranglerConfig>
4343

44-
```jsonc
45-
{
46-
"worker_loaders": [
47-
{
48-
"binding": "LOADER",
49-
},
50-
],
51-
}
44+
```toml
45+
[[worker_loaders]]
46+
binding = "LOADER"
5247
```
5348

5449
</WranglerConfig>
@@ -101,9 +96,9 @@ In this example, `env.LOADER.load()` creates a Dynamic Worker from the code defi
10196

10297
### Reusing a Dynamic Worker across requests
10398

104-
If you expect to load the exact same Worker more than once, use [`get(id, callback)`](/dynamic-workers/api-reference/#get) instead of `load()`. The `id` should be a unique string identifying the particular code you intend to load. When the runtime sees the same `id` again, it can reuse the existing Worker instead of creating a new one, if it hasn't been evicted yet.
99+
If you expect to load the exact same Worker more than once, use [`get(id, callback)`](/dynamic-workers/api-reference/#get) instead of `load()`. The `id` should be a unique string identifying the particular code you intend to load. When the runtime sees the same `id` again, it can reuse the existing Worker instead of creating a new one, if it has not been evicted yet.
105100

106-
The callback you provide will only be called if the Worker is not already loaded. This lets you skip loading the code from storage when the Worker is already running.
101+
The callback you provide only runs if the Worker is not already loaded. This lets you skip loading the code from storage when the Worker is already running.
107102

108103
<TypeScriptExample>
109104

@@ -157,7 +152,7 @@ const worker = env.LOADER.get("my-worker", async () => {
157152
},
158153
});
159154

160-
return { mainModule, modules, compatibilityDate: "2026-01-01" };
155+
return { mainModule, modules, compatibilityDate: "$today" };
161156
});
162157
```
163158

src/content/docs/dynamic-workers/index.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ Spin up Workers at runtime to execute code on-demand in a secure, sandboxed envi
1616

1717
</Description>
1818

19-
Dynamic Workers let you spin up an unlimited number of Workers to execute arbitrary code specified at runtime. Dynamic Workers can be used as a lightweight alternative to containers for securely sandboxing code you don't trust.
19+
Dynamic Workers let you spin up an unlimited number of Workers to execute arbitrary code specified at runtime. Use Dynamic Workers as a lightweight alternative to containers for securely sandboxing code you do not trust.
2020

2121
Dynamic Workers are the lowest-level primitive for spinning up a Worker, giving you full control over defining how the Worker is composed, which bindings it receives, whether it can reach the network, and more.
2222

23-
### Get started
23+
## Get started
2424

2525
Deploy the [Dynamic Workers Playground](https://github.com/cloudflare/agents/tree/main/examples/dynamic-workers-playground) to create and run Workers dynamically from code you write or import from GitHub, with real-time logs and observability.
2626

2727
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/dinasaur404/dynamic-workers-playground)
2828

29-
## Use Dynamic Workers for
29+
## Use cases
3030

31-
Use this pattern when code needs to run quickly in a secure, isolated environment.
31+
Use Dynamic Workers when code needs to run quickly in a secure, isolated environment.
3232

3333
- **AI Agent "Code Mode"**: LLMs are trained to write code. Instead of supplying an agent with tool calls to perform tasks, give it an API and let it write and execute code. Save up to 80% in inference tokens and cost by allowing the agent to programmatically process data instead of sending it all through the LLM.
3434
- **AI-generated applications / "Vibe Code"**: Run generated code for prototypes, projects, and automations in a secure, isolated sandboxed environment.

0 commit comments

Comments
 (0)