Skip to content

Commit

Permalink
[Static Assets] Rename experimental_serve_directly to run_worker_first
Browse files Browse the repository at this point in the history
Updates the naming for the Worker-first configuration.
  • Loading branch information
WillTaylorDev committed Jan 24, 2025
1 parent bc92e8a commit aa6bedf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions public/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,8 @@
/workers/reference/apis/* /workers/runtime-apis/:splat 301
/workers/templates/pages/* /workers/examples/:splat 301
/workers/observability/logging/* /workers/observability/logs/:splat 301
/workers/observability/logging/* /workers/observability/logs/:splat 301
/workers/static-assets/binding/#experimental_serve_directly /workers/static-assets/binding/#run_worker_first 301

# Others
/ssl/custom-certificates/* /ssl/edge-certificates/custom-certificates/:splat 301
Expand Down
14 changes: 7 additions & 7 deletions src/content/docs/workers/static-assets/binding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ _headers

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

## `experimental_serve_directly`
## `run_worker_first`

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).
Controls whether to invoke the Worker script regardless of a request which would have otherwise matched an asset. `run_worker_first = false` ([default](/workers/static-assets/routing/#default-behavior)) will serve any static asset matching a request, while `run_worker_first = true` will unconditionally [invoke your Worker script](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets).

<WranglerConfig>

Expand All @@ -75,7 +75,7 @@ main = "src/index.ts"
[assets]
directory = "./public/"
binding = "ASSETS"
experimental_serve_directly = false
run_worker_first = true
```

</WranglerConfig>
Expand Down Expand Up @@ -167,16 +167,16 @@ For the various static asset routing configuration options, refer to [Routing](/

### Smart Placement with Worker Code First

If you desire to run your [Worker code ahead of assets](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets) by setting `experimental_serve_directly=false`, all requests must first travel to your Smart-Placed Worker. As a result, you may experience increased latency for asset requests.
If you desire to run your [Worker code ahead of assets](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets) by setting `run_worker_first=true`, all requests must first travel to your Smart-Placed Worker. As a result, you may experience increased latency for asset requests.

Use Smart Placement with `experimental_serve_directly=false` when you need to integrate with other backend services, authenticate requests before serving any assets, or if your want to make modifications to your assets before serving them.
Use Smart Placement with `run_worker_first=true` when you need to integrate with other backend services, authenticate requests before serving any assets, or if your want to make modifications to your assets before serving them.

If you want some assets served as quickly as possible to the user, but others to be served behind a smart-placed Worker, considering splitting your app into multiple Workers and [using service bindings to connect them](/workers/configuration/smart-placement/#best-practices).

### Smart Placement with Assets First

Enabling Smart Placement with `experimental_serve_directly=true` lets you serve assets from as close as possible to your users, but moves your Worker logic to run most efficiently (such as near a database).
Enabling Smart Placement with `run_worker_first=false` (or not specifying it) lets you serve assets from as close as possible to your users, but moves your Worker logic to run most efficiently (such as near a database).

Use Smart Placement with `experimental_serve_directly=true` when prioritizing fast asset delivery.
Use Smart Placement with `run_worker_first=false` (or not specifying it) when prioritizing fast asset delivery.

This will not impact the [default routing behavior](/workers/static-assets/routing/#default-behavior).
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate
| **Static Assets** | | |
| [Early Hints](/pages/configuration/early-hints/) |||
| [Custom HTTP headers for static assets](/pages/configuration/headers/) | 🟡 [^1] ||
| [Middleware](/workers/static-assets/binding/#experimental_serve_directly) |[^2] ||
| [Middleware](/workers/static-assets/binding/#run_worker_first) |[^2] ||
| [Redirects](/pages/configuration/redirects/) | 🟡 [^3] ||
| [Smart Placement](/workers/configuration/smart-placement/) |||
| [Serve assets on a path](/workers/static-assets/routing/) |||
Expand Down Expand Up @@ -85,7 +85,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate

[^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.

[^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.
[^2]: Middleware can be configured via the [`run_worker_first`](/workers/static-assets/binding/#run_worker_first) option, but is charged as a normal Worker invocation. We plan to explore additional related options in the future.

[^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/).

Expand Down
6 changes: 3 additions & 3 deletions src/content/docs/workers/static-assets/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ In this example, a request to `example.com/api` doesn't match a static asset so

## Invoking Worker Script Ahead of Assets

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.
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. [`run_worker_first`](/workers/static-assets/binding/#run_worker_first) is a configuration option available in `wrangler.toml` which controls this behavior. When enabled, `run_worker_first = true` will invoke your Worker's code, regardless of any assets that would have otherwise matched.

Take the following directory structure, wrangler.toml, and user Worker code:

Expand All @@ -60,7 +60,7 @@ Take the following directory structure, wrangler.toml, and user Worker code:
name = "my-worker"
compatibility_date = "2024-09-19"
main = "src/index.ts"
assets = { directory = "./public/", binding = "ASSETS", experimental_serve_directly = false }
assets = { directory = "./public/", binding = "ASSETS", run_worker_first = true }
```

</WranglerConfig>
Expand Down Expand Up @@ -91,7 +91,7 @@ export default {
};
```

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.
In this example, any request will be routed to our user Worker, due to `run_worker_first` being enabled. As a result, any request being made `/supersecret.txt` without an `Authorization` header will result in a 403.

## Routing configuration

Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/workers/wrangler/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ The following options are available under the `assets` key.

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

- `experimental_serve_directly` <Type text="boolean" /> <MetaInfo text="optional, defaults to true" />
- `run_worker_first` <Type text="boolean" /> <MetaInfo text="optional, defaults to false" />

- 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).
- Controls whether static assets are fetched directly, or a Worker script is invoked. Learn more about fetching assets when using [`run_worker_first`](/workers/static-assets/routing/#invoking-worker-script-ahead-of-assets).

- `html_handling`: <Type text={'"auto-trailing-slash" | "force-trailing-slash" | "drop-trailing-slash" | "none"'} /> <MetaInfo text={'optional, defaults to "auto-trailing-slash"'} />

Expand Down

0 comments on commit aa6bedf

Please sign in to comment.