Damono 5 months ago export default { async fetch(request, env, ctx) { const url = new URL(request.url); // 圧縮対象とするパスの条件 const isTargetPath = url.pathname.startsWith("/_app/immutable/") && (url.pathname.endsWith(".css") || url.pathname.endsWith(".js") || url.pathname.endsWith(".wasm")); // 対象外はそのままパススルー if (!isTargetPath) { return fetch(request); } const response = await fetch(request); const contentType = response.headers.get("content-type") || ""; // MIMEタイプが明らかに壊れているものを対象外にする(保険) if (!contentType.includes("text/css") && !contentType.includes("application/javascript") && !contentType.includes("application/wasm")) { return response; } // ヘッダーをコピーして content-encoding を gzip に上書き const newHeaders = new Headers(response.headers); newHeaders.set("Content-Encoding", "gzip"); return new Response(response.body, { status: response.status, statusText: response.statusText, headers: newHeaders, }); } }