Fix cache policy for stable public assets
This commit is contained in:
parent
ffb020ee98
commit
75592d7d7a
6 changed files with 171 additions and 40 deletions
67
tests/server-cache.test.mjs
Normal file
67
tests/server-cache.test.mjs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// Rôle : vérifie les en-têtes de cache du serveur Node sur des fichiers de production représentatifs.
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { createStaticServer } from "../server.mjs";
|
||||
|
||||
async function writeFixture(root, path, content) {
|
||||
const file = join(root, path);
|
||||
await mkdir(dirname(file), { recursive: true });
|
||||
await writeFile(file, content);
|
||||
return file;
|
||||
}
|
||||
|
||||
test("production server separates stable files from versioned assets", async (t) => {
|
||||
const root = await mkdtemp(join(tmpdir(), "sokko-server-cache-"));
|
||||
await writeFixture(root, "index.html", "<main>Sokko G</main>");
|
||||
await writeFixture(root, "data/site.json", JSON.stringify({ title: "Sokko G" }));
|
||||
await writeFixture(root, "assets/index-Ab12_cd34.js", "console.log('versioned');");
|
||||
await writeFixture(root, "assets/runtime.js", "console.log('stable');");
|
||||
const imageFile = await writeFixture(root, "static/img/hero.png", "image-v1");
|
||||
|
||||
const server = createStaticServer({ root, publicRoot: root });
|
||||
await new Promise((resolve, reject) => {
|
||||
server.once("error", reject);
|
||||
server.listen(0, "127.0.0.1", resolve);
|
||||
});
|
||||
t.after(async () => {
|
||||
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
||||
await rm(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const address = server.address();
|
||||
const request = (path) => fetch(`http://127.0.0.1:${address.port}${path}`);
|
||||
|
||||
const html = await request("/");
|
||||
assert.equal(html.status, 200);
|
||||
assert.equal(html.headers.get("cache-control"), "no-store");
|
||||
|
||||
const spaFallback = await request("/toolbox/example");
|
||||
assert.equal(spaFallback.status, 200);
|
||||
assert.equal(spaFallback.headers.get("cache-control"), "no-store");
|
||||
|
||||
const json = await request("/data/site.json");
|
||||
assert.equal(json.status, 200);
|
||||
assert.equal(json.headers.get("cache-control"), "no-cache");
|
||||
|
||||
const bundle = await request("/assets/index-Ab12_cd34.js");
|
||||
assert.equal(bundle.status, 200);
|
||||
assert.equal(bundle.headers.get("cache-control"), "public, max-age=31536000, immutable");
|
||||
|
||||
const stableScript = await request("/assets/runtime.js");
|
||||
assert.equal(stableScript.status, 200);
|
||||
assert.equal(stableScript.headers.get("cache-control"), "no-cache");
|
||||
|
||||
const firstImage = await request("/static/img/hero.png");
|
||||
assert.equal(firstImage.headers.get("cache-control"), "no-cache");
|
||||
assert.equal(await firstImage.text(), "image-v1");
|
||||
await writeFile(imageFile, "image-v2");
|
||||
const replacedImage = await request("/static/img/hero.png");
|
||||
assert.equal(await replacedImage.text(), "image-v2");
|
||||
|
||||
const missingBundle = await request("/assets/index-OldHash9.js");
|
||||
assert.equal(missingBundle.status, 404);
|
||||
assert.equal(missingBundle.headers.get("cache-control"), "no-store");
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue