Spaces:
Running

File size: 2,285 Bytes
e4dc8c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { cacheServerHook } from "./cacheServerHook";

let root: string;

beforeAll(() => {
  root = mkdtempSync(path.join(tmpdir(), "cache-hook-"));
  mkdirSync(path.join(root, "dist", "assets"), { recursive: true });
  writeFileSync(path.join(root, "dist", "assets", "index-abc123.js"), "");
  writeFileSync(path.join(root, "dist", "secret.txt"), "");
});

afterAll(() => {
  rmSync(root, { recursive: true, force: true });
});

async function cacheControlFor(url: string) {
  const use = vi.fn();
  cacheServerHook({
    middlewares: { use },
    config: { root, build: { outDir: "dist" } },
  } as never);
  const handler = use.mock.calls[0][0] as (
    req: { url: string },
    res: { setHeader: (name: string, value: string) => void },
    next: () => void,
  ) => Promise<void>;
  const setHeader = vi.fn();
  const next = vi.fn();
  await handler({ url }, { setHeader }, next);
  expect(next).toHaveBeenCalled();
  return setHeader.mock.calls[0][1] as string;
}

describe("cacheServerHook", () => {
  it("marks an existing hashed asset immutable", async () => {
    expect(await cacheControlFor("/assets/index-abc123.js")).toBe(
      "public, max-age=31536000, immutable",
    );
    expect(await cacheControlFor("/assets/index-abc123.js?v=1")).toBe(
      "public, max-age=31536000, immutable",
    );
  });

  it("does not mark a missing asset immutable", async () => {
    expect(await cacheControlFor("/assets/index-OLD.js")).toBe("no-cache");
    expect(await cacheControlFor("/assets/")).toBe("no-cache");
  });

  it("does not look outside the assets folder", async () => {
    expect(await cacheControlFor("/assets/..%2fsecret.txt")).toBe("no-cache");
    expect(await cacheControlFor("/assets/%E0%A4%A.js")).toBe("no-cache");
  });

  it("keeps HTML on no-cache and other files on a one-day cache", async () => {
    expect(await cacheControlFor("/")).toBe("no-cache");
    expect(await cacheControlFor("/index.html")).toBe("no-cache");
    expect(await cacheControlFor("/favicon.png")).toBe(
      "public, max-age=86400, must-revalidate",
    );
  });
});