From 50612e71e13b75e99e4a55f8ecaea8c29fb929f1 Mon Sep 17 00:00:00 2001 From: Daniel Walsh Date: Thu, 12 Dec 2024 12:56:59 +0000 Subject: [PATCH] Serving assets on a path docs --- .../static-assets/compatibility-matrix.mdx | 1 + .../docs/workers/static-assets/routing.mdx | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/content/docs/workers/static-assets/compatibility-matrix.mdx b/src/content/docs/workers/static-assets/compatibility-matrix.mdx index 4415e1e3997a94..ce084b500e0195 100644 --- a/src/content/docs/workers/static-assets/compatibility-matrix.mdx +++ b/src/content/docs/workers/static-assets/compatibility-matrix.mdx @@ -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/) | ✅ | ❌ | diff --git a/src/content/docs/workers/static-assets/routing.mdx b/src/content/docs/workers/static-assets/routing.mdx index f7dd0f1a59f162..e10d70637aa704 100644 --- a/src/content/docs/workers/static-assets/routing.mdx +++ b/src/content/docs/workers/static-assets/routing.mdx @@ -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 */} + +- dist + - blog + - index.html + - posts + - post1.html + - post2.html + + +With a Wrangler configuration file like so: + + +```toml +name = "assets-on-a-path-example" +main = "src/index.js" +route = "example.com/blog/*" + +[assets] +directory = "dist" + +``` + + +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). +```