[ PROMPT_NODE_24330 ]
Static Assets 配置说明
[ SKILL_DOCUMENTATION ]
## 配置
### 基础设置
最小配置仅需 `assets.directory`:
c
{
"name": "my-worker",
"compatibility_date": "2025-01-01", // 新项目请使用当前日期
"assets": {
"directory": "./dist"
}
}
### 完整配置选项
c
{
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2025-01-01",
"assets": {
"directory": "./dist",
"binding": "ASSETS",
"not_found_handling": "single-page-application",
"html_handling": "auto-trailing-slash",
"run_worker_first": ["/api/*", "!/api/docs/*"]
}
}
**配置键:**
- `directory` (字符串, 必需): 资源文件夹路径 (例如 `./dist`, `./public`, `./build`)
- `binding` (字符串, 可选): Worker 代码中访问资源的名称 (例如 `env.ASSETS`)。默认: `"ASSETS"`
- `not_found_handling` (字符串, 可选): 资源未找到时的行为
- `"single-page-application"`: 对非资源路径提供 `/index.html` (SPA 默认)
- `"404-page"`: 如果存在则提供 `/404.html`,否则返回 404
- `"none"`: 对缺失资源返回 404
- `html_handling` (字符串, 可选): URL 尾部斜杠行为
- `run_worker_first` (布尔值 | 字符串数组, 可选): 在检查资源前调用 Worker 的路由
### not_found_handling 模式
| 模式 | 行为 | 使用场景 |
|------|----------|----------|
| `"single-page-application"` | 为非资源请求提供 `/index.html` | React, Vue, Angular SPA |
| `"404-page"` | 如果存在则提供 `/404.html`,否则 404 | 带自定义错误页的静态站点 |
| `"none"` | 对缺失资源返回 404 | API 优先或自定义路由 |
### html_handling 模式
控制 HTML 文件的尾部斜杠行为:
| 模式 | `/page` | `/page/` | 使用场景 |
|------|---------|----------|----------|
| `"auto-trailing-slash"` | 若 `/page/index.html` 存在则重定向至 `/page/` | 提供 `/page/index.html` | 默认,SEO 友好 |
| `"force-trailing-slash"` | 总是重定向至 `/page/` | 若存在则提供 | 保持尾部斜杠一致 |
| `"drop-trailing-slash"` | 若存在则提供 | 重定向至 `/page` | 更简洁的 URL |
| `"none"` | 无修改 | 无修改 | 自定义路由逻辑 |
**默认:** `"auto-trailing-slash"`
### run_worker_first 配置
控制哪些请求在检查资源前调用 Worker。
**布尔语法:**
c
{
"assets": {
"run_worker_first": true // 所有请求调用 Worker
}
}
**数组语法 (推荐):**
c
{
"ass