mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-03 07:30:47 +00:00
Cache the shared app shell HTML
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import fs from "fs/promises";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { afterEach, describe, expect, test } from "vitest";
|
||||
import {
|
||||
clearAppShellContentCache,
|
||||
getAppShellContent,
|
||||
setAppShellCacheHeaders,
|
||||
} from "../../src/server/RenderHtml";
|
||||
|
||||
describe("RenderHtml", () => {
|
||||
const originalGitCommit = process.env.GIT_COMMIT;
|
||||
let tempDir: string | null = null;
|
||||
|
||||
afterEach(async () => {
|
||||
process.env.GIT_COMMIT = originalGitCommit;
|
||||
clearAppShellContentCache();
|
||||
|
||||
if (tempDir) {
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
tempDir = null;
|
||||
}
|
||||
});
|
||||
|
||||
test("reuses cached app shell content", async () => {
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "render-html-"));
|
||||
const htmlPath = path.join(tempDir, "index.html");
|
||||
await fs.writeFile(
|
||||
htmlPath,
|
||||
"<script>window.GIT_COMMIT = <%- gitCommit %>;</script>",
|
||||
"utf8",
|
||||
);
|
||||
|
||||
process.env.GIT_COMMIT = "first";
|
||||
const first = await getAppShellContent(htmlPath);
|
||||
|
||||
process.env.GIT_COMMIT = "second";
|
||||
const second = await getAppShellContent(htmlPath);
|
||||
|
||||
expect(first).toContain('"first"');
|
||||
expect(second).toBe(first);
|
||||
expect(second).not.toContain('"second"');
|
||||
});
|
||||
|
||||
test("sets shared-cache headers for the app shell", () => {
|
||||
const headers = new Map<string, string>();
|
||||
const response = {
|
||||
setHeader(name: string, value: string) {
|
||||
headers.set(name, value);
|
||||
},
|
||||
} as any;
|
||||
|
||||
setAppShellCacheHeaders(response);
|
||||
|
||||
expect(headers.get("Cache-Control")).toBe(
|
||||
"public, max-age=0, s-maxage=300, stale-while-revalidate=86400",
|
||||
);
|
||||
expect(headers.get("Content-Type")).toBe("text/html");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user