Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mounted assets docs #18657

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ We plan to bridge the gaps between Workers and Pages and provide ways to migrate
| [Middleware](/workers/static-assets/binding/#experimental_serve_directly) | ✅ [^2] | ✅ |
| [Redirects](/pages/configuration/redirects/) | 🟡 [^3] | ✅ |
| [Smart Placement](/workers/configuration/smart-placement/) | ✅ | ✅ |
| [Serve assets on a path](/workers/static-assets/routing/) | ✅ | ❌ |
| **Observability** | | |
| [Workers Logs](/workers/observability/) | ✅ | ❌ |
| [Logpush](/workers/observability/logs/logpush/) | ✅ | ❌ |
Expand Down
40 changes: 40 additions & 0 deletions src/content/docs/workers/static-assets/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,43 @@ Take the following directory structure:
/not-found -> 200 /index.html
/folder/doesnt/exist -> 200 /index.html
```

## Serving assets from a custom path

:::note
This feature requires Wrangler v3.98.0 or later.
:::

Like with any other Worker, [you can configure a Worker with assets to run on a path of your domain](/workers/configuration/routing/routes/).
Assets defined for a Worker must be nested in a directory structure that mirrors the desired path.

For example, to serve assets from `example.com/blog/*`, create a `blog` directory in your asset directory.

{/* prettier-ignore */}
<FileTree>
- dist
- blog
- index.html
- posts
- post1.html
- post2.html
</FileTree>

With a Wrangler configuration file like so:

<WranglerConfig>
```toml
name = "assets-on-a-path-example"
main = "src/index.js"
route = "example.com/blog/*"

[assets]
directory = "dist"

```
</WranglerConfig>

In this example, requests to `example.com/blog/` will serve the `index.html` file, and requests to `example.com/blog/posts/post1` will serve the `post1.html` file.

If you have a file outside the configured path, it will not be served. For example, if you have a `home.html` file in the root of your asset directory, it will not be served when requesting `example.com/blog/home`.
However, if needed, these files can still be manually fetched over [the binding](/workers/static-assets/binding/#binding).
Loading