Skip to content

Commit 1535d6e

Browse files
WC-2955 Add documentation for serve_directly usage (#18508)
1 parent 4843f16 commit 1535d6e

File tree

4 files changed

+108
-23
lines changed

4 files changed

+108
-23
lines changed

src/content/docs/workers/static-assets/binding.mdx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
Tabs,
1818
} from "~/components";
1919

20+
import { WranglerConfig } from "~/components";
21+
2022
Configuring a Worker with assets requires specifying a [directory](/workers/static-assets/binding/#directory) and, optionally, an [assets binding](/workers/static-assets/binding/), in your Worker's `wrangler.toml` file. The [assets binding](/workers/static-assets/binding/) allows you to dynamically fetch assets from within your Worker script (e.g. `env.ASSETS.fetch()`), similarly to how you might with a make a `fetch()` call with a [Service binding](/workers/runtime-apis/bindings/service-bindings/http/).
2123

2224
Only one collection of static assets can be configured in each Worker.
@@ -25,8 +27,6 @@ Only one collection of static assets can be configured in each Worker.
2527

2628
The folder of static assets to be served. For many frameworks, this is the `./public/`, `./dist/`, or `./build/` folder.
2729

28-
import { WranglerConfig } from "~/components";
29-
3030
<WranglerConfig>
3131

3232
```toml title="wrangler.toml"
@@ -60,6 +60,23 @@ _headers
6060

6161
Now Wrangler will not upload these files as client-side assets when deploying the Worker.
6262

63+
## `experimental_serve_directly`
64+
65+
Controls whether assets will be served first on a matching request. `experimental_serve_directly = true` ([default](/workers/static-assets/routing/#default-behavior)) will serve any static asset matching a request, while `experimental_serve_directly = false` will unconditionally [invoke your Worker script](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets).
66+
67+
<WranglerConfig>
68+
69+
```toml title="wrangler.toml"
70+
name = "my-worker"
71+
compatibility_date = "2024-09-19"
72+
main = "src/index.ts"
73+
# The following configuration unconditionally invokes the Worker script at
74+
# `src/index.ts`, which can programatically fetch assets via the ASSETS binding
75+
assets = { directory = "./public/", binding = "ASSETS", experimental_serve_directly = false }
76+
```
77+
78+
</WranglerConfig>
79+
6380
## `binding`
6481

6582
Configuring the optional [binding](/workers/runtime-apis/bindings) gives you access to the collection of assets from within your Worker script.

src/content/docs/workers/static-assets/compatibility-matrix.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate
4040
| **Static Assets** | | |
4141
| [Early Hints](/pages/configuration/early-hints/) |||
4242
| [Custom HTTP headers for static assets](/pages/configuration/headers/) | 🟡 [^1] ||
43-
| [Middleware](/pages/functions/middleware/) | 🟡 [^2] ||
43+
| [Middleware](/workers/static-assets/binding/#experimental_serve_directly) | [^2] ||
4444
| [Redirects](/pages/configuration/redirects/) | 🟡 [^3] ||
4545
| [Smart Placement](/workers/configuration/smart-placement/) |||
4646
| **Observability** | | |
@@ -83,7 +83,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate
8383

8484
[^1]: Similar to <sup>3</sup>, to customize the HTTP headers that are returned by static assets, you can use [Service bindings](/workers/runtime-apis/bindings/service-bindings/) to connect a Worker in front of the Worker with assets.
8585

86-
[^2]: If you need to run a Worker before serving static assets, you can create a separate Worker that acts as middleware, and then use [Service bindings](/workers/runtime-apis/bindings/service-bindings/) to forward the request to the Worker with assets. We plan to explore additional configuration to support more complex routing in the future.
86+
[^2]: Middleware can be configured via the [`experimental_serve_directly`](/workers/static-assets/binding/#experimental_serve_directly) option, but is charged as a normal Worker invocation. We plan to explore additional related options in the future.
8787

8888
[^3]: You can handle redirects by adding code to your Worker (a [community package](https://npmjs.com/package/redirects-in-workers) is available for `_redirects` support), or you can use [Bulk Redirects](/rules/url-forwarding/bulk-redirects/).
8989

src/content/docs/workers/static-assets/routing.mdx

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
Tabs,
1818
} from "~/components";
1919

20-
Assets are served by attempting to match up the incoming request's pathname to a static asset. The structure and organization of files in your static asset directory, along with any routing configuration, determine the routing paths for your application. When a request invokes a Worker with assets:
20+
import { WranglerConfig } from "~/components";
21+
22+
## Default behavior
23+
24+
By default, assets are served by attempting to match up the incoming request's pathname to a static asset. The structure and organization of files in your static asset directory, along with any routing configuration, determine the routing paths for your application. When a request invokes a Worker with assets:
2125

2226
1. If a request is found with a matching path to the current route requested then that asset will always be served.
2327

@@ -33,6 +37,62 @@ In this example, request to `example.com/api` doesn't match a static asset so th
3337

3438
![A request to `example.com/blog` runs the Worker.](~/assets/images/workers/platform/assets/workers-assets-invoke-worker.png)
3539

40+
## Invoking Worker Script Ahead of Assets
41+
42+
You may wish to run code before assets are served. This is often the case when implementing authentication, logging, personalization, internationalization, or other similar functions. [`experimental_serve_directly`](/workers/static-assets/binding/#experimental_serve_directly) is a configuration option available in `wrangler.toml` which controls this behavior. When disabled, `experimental_serve_directly = false` will invoke your Worker's code, regardless of any assets that would have otherwise matched.
43+
44+
Take the following directory structure, wrangler.toml, and user Worker code:
45+
46+
<FileTree>
47+
48+
- wrangler.toml
49+
- package.json
50+
- public
51+
- supersecret.txt
52+
- src
53+
- index.ts
54+
55+
</FileTree>
56+
57+
<WranglerConfig>
58+
59+
```toml title="wrangler.toml"
60+
name = "my-worker"
61+
compatibility_date = "2024-09-19"
62+
main = "src/index.ts"
63+
assets = { directory = "./public/", binding = "ASSETS", experimental_serve_directly = false }
64+
```
65+
66+
</WranglerConfig>
67+
68+
```js
69+
export default {
70+
async fetch(
71+
request: Request,
72+
env: Env,
73+
ctx: ExecutionContext
74+
): Promise<Response> {
75+
const url = new URL(request.url);
76+
if (url.pathname === "/supersecret.txt") {
77+
const auth = request.headers.get("Authorization");
78+
if (!auth) {
79+
return new Response("Forbidden", {
80+
status: 403,
81+
statusText: "Forbidden",
82+
headers: {
83+
"Content-Type": "text/plain",
84+
},
85+
});
86+
}
87+
}
88+
89+
return await env.ASSETS.fetch(request);
90+
},
91+
};
92+
```
93+
94+
In this example, any request will be routed to our user Worker, due to `experimental_serve_directly` being disabled. As a result, any request being made `/supersecret.txt` without an `Authorization` header will result in a 403.
95+
3696
## Routing configuration
3797

3898
There are two options for asset serving that can be [configured in `wrangler.toml`](/workers/wrangler/configuration/#assets):
@@ -61,11 +121,13 @@ If you have custom 404 HTML pages, and configure `not_found_handling` to `"404-p
61121

62122
Take the following directory structure:
63123

64-
```
65-
|---- file.html
66-
|---- folder
67-
|___ index.html
68-
```
124+
<FileTree>
125+
126+
- file.html
127+
- folder
128+
- index.html
129+
130+
</FileTree>
69131

70132
Based on the incoming requests, the following assets would be served:
71133

@@ -99,8 +161,6 @@ Alternate configuration options are outlined on this page and can be specified i
99161

100162
Example `wrangler.toml` configuration:
101163

102-
import { WranglerConfig } from "~/components";
103-
104164
<WranglerConfig>
105165

106166
```toml title="wrangler.toml"
@@ -113,11 +173,13 @@ assets = { directory = "./public", binding = "ASSETS", html_handling = "force-tr
113173

114174
Take the following directory structure:
115175

116-
```
117-
|---- file.html
118-
|---- folder
119-
|___ index.html
120-
```
176+
<FileTree>
177+
178+
- file.html
179+
- folder
180+
- index.html
181+
182+
</FileTree>
121183

122184
**`html_handling: "auto-trailing-slash"`**
123185

@@ -191,12 +253,14 @@ Based on the incoming requests, the following assets would be served:
191253

192254
Take the following directory structure:
193255

194-
```
195-
|---- 404.html
196-
|---- index.html
197-
|---- folder
198-
|___ 404.html
199-
```
256+
<FileTree>
257+
258+
- 404.html
259+
- index.html
260+
- folder
261+
- 404.html
262+
263+
</FileTree>
200264

201265
**`not_found_handling: "none"`**
202266

src/content/docs/workers/wrangler/configuration.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,10 @@ The following options are available under the `assets` key.
981981

982982
- The binding name used to refer to the assets. Optional, and only useful when a Worker script is set with `main`.
983983

984+
- `experimental_serve_directly` <Type text="boolean" /> <MetaInfo text="optional, defaults to true" />
985+
986+
- Controls whether static assets are fetched directly, or a Worker script is invoked. Learn more about fetching assets when using [`experimental_serve_directly`](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets).
987+
984988
- `html_handling`: <Type text={'"auto-trailing-slash" | "force-trailing-slash" | "drop-trailing-slash" | "none"'} /> <MetaInfo text={'optional, defaults to "auto-trailing-slash"'} />
985989

986990
- Determines the redirects and rewrites of requests for HTML content. Learn more about the various options in [assets routing](/workers/static-assets/routing/#html_handling).

0 commit comments

Comments
 (0)