Compresses responses in the preview server

The baseline ran on vite preview, which compresses, so measurements could not
be compared without this. Also records that the real servers need compression.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 18:30:30 +01:00
co-authored by Claude Opus 5.5
parent fe1ec5db3d
commit 0c5d8b8f40
6 changed files with 220 additions and 15 deletions
+50 -7
View File
@@ -14,6 +14,7 @@ import {
} from 'node:http';
import { extname, join, resolve, sep, win32 } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createGzip } from 'node:zlib';
import { NOT_FOUND_FILE, pageFile } from './pageFiles.ts';
import { securityHeaders } from './securityHeaders.ts';
@@ -92,6 +93,41 @@ const CONTENT_TYPES: Record<string, string> = {
export const contentTypeOf = (file: string): string =>
CONTENT_TYPES[extname(file).toLowerCase()] ?? 'application/octet-stream';
// Tekst wordt gecomprimeerd, afbeeldingen en lettertypen niet (die zijn het al).
// Waarom: de nulmeting draaide op `vite preview`, dat comprimeert; zonder dit
// zijn de Lighthouse-metingen niet te vergelijken (scripts: 667 kB tegen
// 202 kB, gevonden in stap 24). Productie comprimeert op de host (BR-SRV-08).
const COMPRESSIBLE = new Set([
'.html',
'.js',
'.css',
'.svg',
'.json',
'.xml',
'.txt',
]);
export const isCompressible = (file: string): boolean =>
COMPRESSIBLE.has(extname(file).toLowerCase());
// Staat gzip in Accept-Encoding? 'gzip;q=0' weigert het, '*' staat het toe,
// tenzij gzip zelf genoemd is
export function acceptsGzip(header: string | undefined): boolean {
if (!header) return false;
let gzip: number | undefined;
let star: number | undefined;
for (const part of header.split(',')) {
const [name, ...params] = part.trim().toLowerCase().split(';');
const q = params
.map((param) => /^\s*q\s*=\s*([0-9.]+)\s*$/.exec(param))
.find(Boolean);
const weight = q ? Number(q[1]) : 1;
if (name === 'gzip') gzip = weight;
else if (name === '*') star = weight;
}
return (gzip ?? star ?? 0) > 0;
}
// Alleen het pad: query en hash horen niet bij het bestand. Bewust niet via
// `new URL`, want die haalt '..' al weg en dan ziet resolveRequest het niet.
const pathOf = (target: string) => target.split(/[?#]/, 1)[0];
@@ -151,15 +187,22 @@ export function startPreviewServer(
}
const headers: Record<string, string | number> = {
'Content-Type': contentTypeOf(file),
'Content-Length': statSync(target).size,
};
if (file.endsWith('.html')) headers['Cache-Control'] = 'no-cache';
send(
response,
status,
headers,
request.method === 'HEAD' ? null : createReadStream(target),
);
// Gzip als stroom: de lengte is vooraf niet bekend, dus dan geen
// Content-Length (chunked); ook niet bij HEAD, dat geen inhoud stuurt
const compressible = isCompressible(file);
const gzip =
compressible && acceptsGzip(request.headers['accept-encoding']);
if (compressible) headers['Vary'] = 'Accept-Encoding';
if (gzip) headers['Content-Encoding'] = 'gzip';
else headers['Content-Length'] = statSync(target).size;
let body: NodeJS.ReadableStream | null = null;
if (request.method !== 'HEAD') {
body = createReadStream(target);
if (gzip) body = body.pipe(createGzip());
}
send(response, status, headers, body);
};
return new Promise((resolvePromise, reject) => {