38 lines
3.5 KiB
JavaScript
38 lines
3.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFileSync, statSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
const distDirectory = join(process.cwd(), "apps", "web", "dist");
|
|
const indexHtml = readFileSync(join(distDirectory, "index.html"), "utf8");
|
|
const serviceWorker = readFileSync(join(distDirectory, "sw.js"), "utf8");
|
|
const staticHeaders = readFileSync(join(distDirectory, "_headers"), "utf8");
|
|
assert.match(serviceWorker, /edgeever-resource-blobs/, "PWA must provide a runtime cache for resource bytes");
|
|
assert.match(serviceWorker, /CacheFirst/, "PWA resource bytes must use a cache-first runtime strategy");
|
|
assert.match(serviceWorker, /edgeever-app-shell/, "PWA navigation must use a dedicated app-shell runtime cache");
|
|
assert.match(serviceWorker, /NetworkFirst/, "PWA navigation must prefer the current deployment over cached HTML");
|
|
assert.match(serviceWorker, /PrecacheFallbackPlugin/, "PWA navigation must retain an offline app-shell fallback");
|
|
assert.doesNotMatch(serviceWorker, /NavigationRoute/, "PWA navigation must not always serve the precached HTML shell");
|
|
assert.match(staticHeaders, /\/sw\.js\s+Cache-Control: no-cache, no-store, must-revalidate/, "Service worker updates must not use stale browser cache");
|
|
assert.match(staticHeaders, /\/assets\/\*\s+Cache-Control: public, max-age=31536000, immutable/, "Fingerprinted assets must use immutable browser caching");
|
|
const precacheStart = serviceWorker.indexOf("precacheAndRoute(");
|
|
const precacheEnd = serviceWorker.indexOf(");", precacheStart);
|
|
assert.ok(precacheStart >= 0 && precacheEnd > precacheStart, "Web service worker must contain a precache manifest");
|
|
|
|
const precacheManifest = serviceWorker.slice(precacheStart, precacheEnd);
|
|
assert.doesNotMatch(precacheManifest, /\{url:"index\.html",/, "Current HTML must not be served by a cache-first precache route");
|
|
assert.match(precacheManifest, /index\.html\?edgeever-offline-shell=/, "PWA must retain a versioned offline HTML shell");
|
|
const optionalDiagramPattern = /(?:vendor-(?:beautiful-mermaid|mermaid)|mermaid\.core|[^"']*Diagram-)[^"']*\.js/;
|
|
assert.doesNotMatch(precacheManifest, optionalDiagramPattern, "Optional diagram chunks must remain out of the initial PWA precache");
|
|
|
|
const entryCount = (precacheManifest.match(/\{url:/g) ?? []).length;
|
|
assert.ok(entryCount > 0, "Web service worker precache manifest must not be empty");
|
|
const modulePreloads = indexHtml.match(/<link rel="modulepreload"[^>]+>/g)?.join("\n") ?? "";
|
|
const initialOptionalPattern = /vendor-code-highlight|vendor-D3|vendor-(?:beautiful-mermaid|mermaid|tiptap|prosemirror|floating)|ui-primitives|mermaid\.core|[^"']*Diagram-/;
|
|
assert.doesNotMatch(modulePreloads, initialOptionalPattern, "Optional editor and diagram chunks must remain out of the initial HTML modulepreload list");
|
|
const initialModulePreloadBytes = [...indexHtml.matchAll(/<link rel="modulepreload"[^>]+href="([^"]+)"[^>]*>/g)]
|
|
.map((match) => statSync(join(distDirectory, match[1].replace(/^\//, ""))).size)
|
|
.reduce((total, size) => total + size, 0);
|
|
const INITIAL_MODULE_PRELOAD_BUDGET = 700 * 1024;
|
|
assert.ok(initialModulePreloadBytes <= INITIAL_MODULE_PRELOAD_BUDGET, `Initial modulepreload budget exceeded: ${initialModulePreloadBytes} > ${INITIAL_MODULE_PRELOAD_BUDGET}`);
|
|
console.log(JSON.stringify({ ok: true, precacheEntries: entryCount, initialModulePreloadBytes, initialModulePreloadBudget: INITIAL_MODULE_PRELOAD_BUDGET, resourceBytesCache: "cache-first", optionalDiagramChunksDeferred: true, optionalInitialChunksDeferred: true }));
|