Skip to content

Commit 5db1985

Browse files
committed
WC-2955 Add documentation for serve_directly usage
1 parent 5a61168 commit 5db1985

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

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

Lines changed: 19 additions & 4 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"
@@ -37,11 +37,26 @@ assets = { directory = "./public/" }
3737

3838
</WranglerConfig>
3939

40-
## `binding`
40+
## `serve_directly`
4141

42-
Configuring the optional [binding](/workers/runtime-apis/bindings) gives you access to the collection of assets from within your Worker script.
42+
Controls whether assets will be served first on a matching request. `serve_directly = true` ([default](/workers/static-assets/routing/#default-behavior)) will serve any static asset matching a request, while `serve_directly = false` will unconditionally [invoke your Worker script](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets).
4343

44+
<WranglerConfig>
4445

46+
```toml title="wrangler.toml"
47+
name = "my-worker"
48+
compatibility_date = "2024-09-19"
49+
main = "src/index.ts"
50+
# The following configuration unconditionally invokes the Worker script at
51+
# `src/index.ts`, which can programatically fetch assets via the ASSETS binding
52+
assets = { directory = "./public/", binding = "ASSETS", serve_directly = false }
53+
```
54+
55+
</WranglerConfig>
56+
57+
## `binding`
58+
59+
Configuring the optional [binding](/workers/runtime-apis/bindings) gives you access to the collection of assets from within your Worker script.
4560

4661
<WranglerConfig>
4762

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate
8585

8686
[^2]: 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.
8787

88-
[^3]: 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.
88+
[^3]: Middleware can be configured by configuring the `[serve_directly](/workers/static-assets/binding/#serve_directly)` option, but is charged as a normal Worker invocation. We plan to explore options for free middleware in the future.
8989

9090
[^4]: 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/).
9191

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

Lines changed: 57 additions & 3 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,58 @@ 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+
Users may desire to run code before assets are served, much like [middleware in Pages](/pages/functions/middleware/). [`serve_directly`](/workers/static-assets/binding/#serve_directly) is provided as a configuration within wrangler.toml. When disabled, this option forces a user script to be invoked regardless of a request matching an asset.
43+
44+
Take the following directory structure, wrangler.toml, and user Worker code:
45+
46+
```
47+
|---- src
48+
|___ index.ts
49+
|---- public
50+
|___ supersecret.html
51+
```
52+
53+
<WranglerConfig>
54+
55+
```toml title="wrangler.toml"
56+
name = "my-worker"
57+
compatibility_date = "2024-09-19"
58+
main = "src/index.ts"
59+
assets = { directory = "./public/", binding = "ASSETS", serve_directly = false }
60+
```
61+
62+
</WranglerConfig>
63+
64+
```js
65+
export default {
66+
async fetch(
67+
request: Request,
68+
env: Env,
69+
ctx: ExecutionContext
70+
): Promise<Response> {
71+
const url = new URL(request.url);
72+
if (url.pathname === "/supersecret.html") {
73+
const auth = request.headers.get("Authorization");
74+
if (!auth) {
75+
return new Response("Forbidden", {
76+
status: 403,
77+
statusText: "Forbidden",
78+
headers: {
79+
"Content-Type": "text/plain",
80+
},
81+
});
82+
}
83+
}
84+
85+
return await env.ASSETS.fetch(request);
86+
},
87+
};
88+
```
89+
90+
In this example, any request will be routed to our user Worker, due to `serve_directly` being disabled. As a result, any request being made `/supersecret.html` without an `Authorization` header will result in a 403.
91+
3692
## Routing configuration
3793

3894
There are two options for asset serving that can be [configured in `wrangler.toml`](/workers/wrangler/configuration/#assets):
@@ -99,8 +155,6 @@ Alternate configuration options are outlined on this page and can be specified i
99155

100156
Example `wrangler.toml` configuration:
101157

102-
import { WranglerConfig } from "~/components";
103-
104158
<WranglerConfig>
105159

106160
```toml title="wrangler.toml"

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

Lines changed: 5 additions & 1 deletion
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+
- `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 [`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).
@@ -1191,7 +1195,7 @@ upload_source_maps = true
11911195

11921196
## Workers Sites
11931197

1194-
<Render file="workers_sites"/>
1198+
<Render file="workers_sites" />
11951199

11961200
[Workers Sites](/workers/configuration/sites/) allows you to host static websites, or dynamic websites using frameworks like Vue or React, on Workers.
11971201

0 commit comments

Comments
 (0)