设置 assets.base_path 可从 URL 子路径提供静态资产。文件仍位于 assets.directory 的根目录。
例如,使用此资产目录提供博客:
- dist
- index.html
- posts
- post1.html
- post2.html
在 Wrangler 配置文件中将 assets.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"
}
}"$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"对 example.com/blog/ 的请求会提供 dist/index.html。对 example.com/blog/posts/post1 的请求会提供 dist/posts/post1.html。
/blog 之外的请求不会解析静态资产。通过资产绑定发出的请求也使用挂载的公共路径。转发原始请求无需任何更改。构造请求时,请在路径名中包含 /blog,例如 env.ASSETS.fetch("https://assets.local/blog/posts/post1")。
_headers、_redirects 和 assets.run_worker_first 中的路径使用公共 URL 路径。当这些路径适用于此示例时,请在其中包含 /blog。