Set immutable cache headers for built static assets
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
7957e29cbf
commit
271d78a9a5
2 files changed
+30
-6
No files matched your search
@@ -175,6 +175,26 @@ async function main() {
|
||||
)}.`,
|
||||
);
|
||||
}
|
||||
const html = await page.text();
|
||||
const assetMatch = html.match(/(?:src|href)="(\/assets\/[^"]+)"/);
|
||||
if (!assetMatch?.[1]) {
|
||||
throw new Error("Smoke test failed: could not locate built asset path in HTML.");
|
||||
}
|
||||
const assetUrl = new URL(assetMatch[1], parsedAppUrl);
|
||||
const assetResponse = await fetch(assetUrl);
|
||||
if (assetResponse.status !== 200) {
|
||||
throw new Error(
|
||||
`Smoke test failed: expected built asset status 200, received ${assetResponse.status}.`,
|
||||
);
|
||||
}
|
||||
const assetCacheControl = (assetResponse.headers.get("cache-control") ?? "").toLowerCase();
|
||||
if (!assetCacheControl.includes("immutable")) {
|
||||
throw new Error(
|
||||
`Smoke test failed: expected immutable cache-control on built asset, got ${String(
|
||||
assetResponse.headers.get("cache-control"),
|
||||
)}.`,
|
||||
);
|
||||
}
|
||||
const missingAssetUrl = new URL("/assets/missing-bundle.js", parsedAppUrl);
|
||||
const missingAsset = await fetch(missingAssetUrl);
|
||||
if (missingAsset.status !== 404) {
|
||||
|
||||
+10
-6
@@ -432,6 +432,14 @@ function contentTypeFor(filePath: string): string {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
function cacheControlFor(filePath: string): string {
|
||||
if (filePath.endsWith(".html")) {
|
||||
return "no-store";
|
||||
}
|
||||
|
||||
return "public, max-age=31536000, immutable";
|
||||
}
|
||||
|
||||
function isPathInside(parent: string, child: string): boolean {
|
||||
const relative = path.relative(parent, child);
|
||||
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
||||
@@ -619,9 +627,7 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
response.setHeader("Content-Type", contentTypeFor(targetPath));
|
||||
response.setHeader("Content-Length", String(stats.size));
|
||||
response.setHeader("X-Content-Type-Options", "nosniff");
|
||||
if (targetPath.endsWith(".html")) {
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
}
|
||||
response.setHeader("Cache-Control", cacheControlFor(targetPath));
|
||||
response.end();
|
||||
});
|
||||
return;
|
||||
@@ -637,9 +643,7 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
response.setHeader("Content-Type", contentTypeFor(targetPath));
|
||||
response.setHeader("X-Content-Type-Options", "nosniff");
|
||||
response.setHeader("Content-Length", String(content.byteLength));
|
||||
if (targetPath.endsWith(".html")) {
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
}
|
||||
response.setHeader("Cache-Control", cacheControlFor(targetPath));
|
||||
response.end(content);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user