perf: accelerate production request hot paths
This commit is contained in:
@@ -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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user