perf: accelerate production request hot paths
Quality / quality (ubuntu-latest) (push) Failing after 12m23s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
2026-08-03 01:08:43 +05:30
parent fed1d5d3f4
commit 1a1d2e9d08
4 changed files with 164 additions and 8 deletions
@@ -1,4 +1,7 @@
import { expect, test } from "bun:test";
import { mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createProductionHandlers, type ProdManifest } from "../src/prod.ts";
const manifest: ProdManifest = {
@@ -72,3 +75,49 @@ test("production client-load endpoint returns only the requested named result",
expect(response.headers.get("cache-control")).toBe("private, no-store");
expect(await response.json()).toEqual({ data: [{ id: 1, name: "Ada" }] });
});
test("production prefers fast gzip for dynamic HTML and cached Brotli for immutable assets", async () => {
const largeManifest: ProdManifest = {
...manifest,
pages: [
{
raw: "/large",
mod: { default: () => `<main>${"fast production ".repeat(500)}</main>` },
},
],
};
const directory = mkdtempSync(join(tmpdir(), "wrnexus-prod-assets-"));
const assetPath = join(directory, "app.js");
writeFileSync(assetPath, "const production = true;\n".repeat(500));
const handlers = createProductionHandlers(largeManifest, {
pluginAssets: {
"/assets/app.js": {
path: assetPath,
contentType: "text/javascript; charset=utf-8",
},
},
});
const headers = { "accept-encoding": "br, gzip" };
const page = (await handlers.fetch(
new Request("http://localhost/large", { headers }),
server,
)) as Response;
expect(page.headers.get("content-encoding")).toBe("gzip");
const firstAsset = (await handlers.fetch(
new Request("http://localhost/assets/app.js", { headers }),
server,
)) as Response;
const secondAsset = (await handlers.fetch(
new Request("http://localhost/assets/app.js", { headers }),
server,
)) as Response;
expect(firstAsset.headers.get("content-encoding")).toBe("br");
expect(await secondAsset.arrayBuffer()).toEqual(await firstAsset.arrayBuffer());
const head = (await handlers.fetch(
new Request("http://localhost/assets/app.js", { method: "HEAD", headers }),
server,
)) as Response;
expect(head.headers.get("content-encoding")).toBeNull();
});