import { readFile } from "node:fs/promises"; import { expect, test } from "@playwright/test"; import { trackPreloadUse } from "../support/browser/preload-use.ts"; import { test as strictTest } from "../support/browser/strict-browser-test.ts"; /** * 1103/11-09. The strict browser fixture ignores WebKit's "preloaded using * link preload but not used within a few seconds" warning. WebKit checks once, * three seconds after the load event, so a preload whose module the page * reaches a moment later was reported as wasted, and one the page never runs * went unreported when the test ended first. The fixture checks instead, in * every engine, that each preloaded script was run by the end of the test. * These tests prove that check on a real build. */ type ManifestEntry = Readonly<{ file: string; imports?: readonly string[]; isEntry?: boolean; }>; async function buildManifest(): Promise>> { return JSON.parse(await readFile("dist/.vite/manifest.json", "utf8")) as Record< string, ManifestEntry >; } function staticClosure( manifest: Readonly>, roots: readonly string[], ): Set { const reached = new Set(); const pending = [...roots]; for (let key = pending.pop(); key !== undefined; key = pending.pop()) { if (reached.has(key) || !manifest[key]) continue; reached.add(key); pending.push(...(manifest[key].imports ?? [])); } return reached; } test("reports a modulepreload the page never runs", async ({ page }) => { const preloads = await trackPreloadUse(page); await page.goto("/"); await expect(page.getByRole("heading", { level: 1 })).toBeVisible(); const scripts = Object.values(await buildManifest()) .map((entry) => `/${entry.file}`) .filter((file) => file.endsWith(".js")); // Mutation: one chunk the page has not fetched, preloaded for nothing. const unused = await page.evaluate(async (files) => { const fetched = new Set( performance .getEntriesByType("resource") .map((entry) => new URL(entry.name).pathname), ); const file = files.find((candidate) => !fetched.has(candidate)); if (file === undefined) throw new Error("every build chunk was fetched"); const link = document.createElement("link"); link.rel = "modulepreload"; link.href = file; document.head.append(link); return file; }, scripts); await page.waitForFunction( (file) => performance .getEntriesByType("resource") .some((entry) => new URL(entry.name).pathname === file), unused, ); expect((await preloads.check({ timeoutMs: 1_000 })).failures).toEqual([ `preloadunused:${unused}`, ]); }); test("counts the chunks a route preloads and then runs as used", async ({ page, }) => { const preloads = await trackPreloadUse(page); await page.goto("/"); await page .getByRole("navigation", { name: "주요 탐색" }) .getByRole("link", { name: "플랫폼 구성" }) .click(); await expect( page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }), ).toBeVisible(); const result = await preloads.check(); expect(result.preloaded.length).toBeGreaterThan(0); expect(result.failures).toEqual([]); }); /** * The 1103/11-09 failure, made deterministic: the route's runtime chunks are * preloaded right after load, and the chunk that imports the deepest one * arrives 3.5 s later. WebKit's check runs in between and warns about a chunk * the page then runs. The strict fixture must pass. */ strictTest( "passes a route whose deepest preloaded chunk is reached after WebKit's check", async ({ page }) => { const manifest = await buildManifest(); const initial = staticClosure(manifest, [ ...Object.keys(manifest).filter((key) => manifest[key]?.isEntry), "src/bootstrap/boot.tsx", ]); // A feature runtime's static import that itself imports a chunk the // boot has not loaded: the module graph reaches that chunk only once // this one has arrived. const parent = Object.entries(manifest) .filter(([key]) => /^src\/features\/[^/]+\/feature-runtime\.ts$/u.test(key)) .flatMap(([, runtime]) => runtime.imports ?? []) .find( (key) => !initial.has(key) && (manifest[key]?.imports ?? []).some((child) => !initial.has(child)), ); await page.goto("/"); if (parent !== undefined) { const file = `/${manifest[parent]?.file}`; await page.route( (url) => url.pathname === file, async (route) => { await new Promise((resolve) => setTimeout(resolve, 3_500)); await route.fallback(); }, ); } await page .getByRole("navigation", { name: "주요 탐색" }) .getByRole("link", { name: "플랫폼 구성" }) .click(); await expect( page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }), ).toBeVisible({ timeout: 15_000 }); }, );