Skip to content

Serving a subdirectory

Last updated View as MarkdownAgent setup

Set assets.base_path to serve static assets from a URL subpath. The files remain at the root of assets.directory.

For example, use this asset directory to serve a blog:

  • dist
    • index.html
    • posts
      • post1.html
      • post2.html

Set assets.base_path to /blog in your Wrangler configuration file:

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "assets-on-a-path-example",
	"main": "src/index.js",
	"upload_source_maps": true,
	"route": "example.com/blog/*",
	"assets": {
		"directory": "dist",
		"base_path": "/blog"
	}
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "assets-on-a-path-example"
main = "src/index.js"
upload_source_maps = true
route = "example.com/blog/*"

[assets]
directory = "dist"
base_path = "/blog"

Requests to example.com/blog/ serve dist/index.html. Requests to example.com/blog/posts/post1 serve dist/posts/post1.html.

Requests outside /blog do not resolve static assets. Requests made through the assets binding also use the mounted public path. Forwarding the original request works unchanged. When constructing a request, include /blog in its pathname, for example, env.ASSETS.fetch("https://assets.local/blog/posts/post1").

Paths in _headers, _redirects, and assets.run_worker_first use the public URL path. Include /blog in those paths when they apply to this example.

Was this helpful?