正確配置靜態資源緩存過期時間,能顯著減少回源請求、降低帶寬成本、提升頁面加載速度。IIS 提供 GUI 方式設置站點級/目錄級/文件級 `Cache-Control: max-age` 與 `Expires` 頭。
| 資源類型 | 建議過期時間 | 單位選擇 | 生成頭示例 |
|---|---|---|---|
| 靜態資源 (JS/CSS/圖片/字體/視頻) - 含版本戳/哈希文件名 | 365 天 (1 年) | 天 | Cache-Control: max-age=31536000 |
| 靜態資源 - 無版本控制 | 30 天 | 天 | Cache-Control: max-age=2592000 |
| 半動態頁面 (個性化推薦片段) | 5-10 分鐘 | 分鐘 | Cache-Control: max-age=300 |
| API 回應 (允許緩存) | 1-5 分鐘 | 分鐘/秒 | Cache-Control: max-age=60 |
| 不應緩存 (登入頁/支付/即時數據) | 立即過期 | 選「立即過期」 | Cache-Control: no-cache, no-store, must-revalidate |
GUI 僅支援 `max-age`/`Expires`。若需添加 `public`/`private`/`must-revalidate`/`stale-while-revalidate` 等指令,需在 `web.config` 中配置:
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" cacheControlCustom="public, must-revalidate" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="public, max-age=31536000, stale-while-revalidate=86400" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
curl -I https://example.com/static/app.js 檢查響應頭。