[ PROMPT_NODE_24328 ]
Static Assets API 参考
[ SKILL_DOCUMENTATION ]
# API 参考
## ASSETS 绑定
`ASSETS` 绑定通过 `Fetcher` 接口提供对静态资源的访问。
### 类型定义
typescript
interface Env {
ASSETS: Fetcher;
}
interface Fetcher {
fetch(input: RequestInfo | URL, init?: RequestInit): Promise;
}
### 方法签名
typescript
// 1. 转发整个请求
await env.ASSETS.fetch(request);
// 2. 字符串路径 (主机名被忽略,仅路径有效)
await env.ASSETS.fetch("https://any-host/path/to/asset.png");
// 3. URL 对象
await env.ASSETS.fetch(new URL("/index.html", request.url));
// 4. 构建的 Request 对象
await env.ASSETS.fetch(new Request(new URL("/logo.png", request.url), {
method: "GET",
headers: request.headers
}));
**关键行为:**
- 字符串/URL 输入中的主机/源站会被忽略(仅使用路径)
- 方法必须为 GET(其他方法返回 405)
- 请求头会透传(影响响应)
- 返回标准的 `Response` 对象
## 请求处理
### 路径解析
typescript
// 全部解析为同一资源:
env.ASSETS.fetch("https://example.com/logo.png")
env.ASSETS.fetch("https://ignored.host/logo.png")
env.ASSETS.fetch("/logo.png")
资源相对于配置的 `assets.directory` 进行解析。
### 请求头
影响响应的请求头:
| 请求头 | 影响 |
|--------|--------|
| `Accept-Encoding` | 控制压缩 (gzip, brotli) |
| `Range` | 启用部分内容 (206 响应) |
| `If-None-Match` | 通过 ETag 进行条件请求 |
| `If-Modified-Since` | 通过修改日期进行条件请求 |
自定义请求头会透传,但不会影响资源服务。
### 方法支持
| 方法 | 支持 | 响应 |
|--------|-----------|----------|
| `GET` | ✅ 是 | 资源内容 |
| `HEAD` | ✅ 是 | 仅响应头,无主体 |
| `POST`, `PUT`, 等 | ❌ 否 | 405 Method Not Allowed |
## 响应行为
### Content-Type 推断
根据文件扩展名自动设置:
| 扩展名 | Content-Type |
|-----------|--------------|
| `.html` | `text/html; charset=utf-8` |
| `.css` | `text/css` |
| `.js` | `application/javascript` |
| `.json` | `application/json` |
| `.png` | `image/png` |
| `.jpg`, `.jpeg` | `image/jpeg` |
| `.svg` | `image/svg+xml` |
| `.woff2` | `font/woff2` |
### 默认响应头
响应包含:
Content-Type:
ETag: ""
Cache-Control: public, max-age=3600
Content-Encoding: br (如果支持且有益)
**Cache-Control 默认值:**
- 1 小时 (`max-age=3600`) fo