headers->has('Cache-Control')) { $cacheControl = $response->headers->get('Cache-Control'); if (str_contains($cacheControl, 'no-cache')) { // 策略:HTML 頁面用短快取(5 分鐘),靜態資源用長快取(7 天) // 原因:HTML 包含對帶 hash 檔名的引用,重建後需要快速更新 $isHtmlPage = str_contains($response->headers->get('Content-Type', ''), 'text/html'); if ($isHtmlPage) { // HTML 頁面:5 分鐘快取(足夠減少伺服器負載,又能快速更新) $response->headers->set('Cache-Control', 'public, max-age=300, must-revalidate'); } else { // 靜態資源(API JSON responses):7 天快取 $response->headers->set('Cache-Control', 'public, max-age=604800'); } } } return $response; } }