Skip to content

Commit

Permalink
Serving assets on a path docs
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Dec 12, 2024
1 parent 2708df3 commit 50612e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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](/pages/functions/middleware/) | 🟡 [^3] ||
| [Redirects](/pages/configuration/redirects/) | 🟡 [^4] ||
| [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 @@ -218,3 +218,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.xx.xx 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).
```

0 comments on commit 50612e7

Please sign in to comment.