Optimize static HEAD responses with content-length

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 00:56:32 +00:00
1 parent 678da637a6
commit ead26b4c26
2 files changed
+24 -4

No files matched your search

+8
View File
@@ -174,6 +174,14 @@ async function main() {
`Smoke test failed: expected HEAD web status 200, received ${headPage.status}.`,
);
}
const headContentLength = Number(headPage.headers.get("content-length") ?? "0");
if (!Number.isFinite(headContentLength) || headContentLength <= 0) {
throw new Error(
`Smoke test failed: expected positive content-length for HEAD response, got ${String(
headPage.headers.get("content-length"),
)}.`,
);
}
const wsUrl = parsedAppUrl.searchParams.get("ws");
if (!wsUrl) {
+16 -4
View File
@@ -564,6 +564,21 @@ function startStaticWebServer(distRoot: string, port: number) {
}
const targetPath = resolvedPath.filePath;
if (requestMethod === "HEAD") {
fs.stat(targetPath, (error, stats) => {
if (error || !stats.isFile()) {
respondText(404, "Not found");
return;
}
response.statusCode = 200;
response.setHeader("Content-Type", contentTypeFor(targetPath));
response.setHeader("Content-Length", String(stats.size));
response.setHeader("X-Content-Type-Options", "nosniff");
response.end();
});
return;
}
fs.readFile(targetPath, (error, content) => {
if (error) {
respondText(404, "Not found");
@@ -573,10 +588,7 @@ function startStaticWebServer(distRoot: string, port: number) {
response.statusCode = 200;
response.setHeader("Content-Type", contentTypeFor(targetPath));
response.setHeader("X-Content-Type-Options", "nosniff");
if (requestMethod === "HEAD") {
response.end();
return;
}
response.setHeader("Content-Length", String(content.byteLength));
response.end(content);
});
});