문제: SW 자산 생성기(scripts/generate-service-worker-assets.ts)가 사전
캐시 URL을 `/${relative}`로 만들어 VITE_ROUTER_BASE_PATH를 무시했다.
워커는 root-relative URL을 scope 기준으로 풀지만 base가 사라지므로
/app/ 배포에서 install이 /assets/...를 요청해 404를 받고, 워커가
설치되지 않았다. 같은 빌드의 SW 설정도 runtime config URL 기본값을
"/config.json"으로 고정해, 페이지가 쓰는 /app/config.json을 워커가
network-only로 알아보지 못했다. 앱 빌드·runtime config·06-08
fallback은 이미 base를 따르는데 이 두 곳만 빠져 있었다.
고른 방법: 리뷰가 적은 최소 방향. collectStaticAssets에 basePath
인자(기본 "/")를 더하고, 생성기 main과 SW Vite 설정이 페이지와 같은
getBuildConfig(src/contracts/env.ts)로 base와 runtime config URL을
계산한다. 결과 URL은 그대로 root-relative라 isCanonicalStaticAssetUrl
검사를 통과한다. base가 "/"이면 manifest와 digest가 전과 같다.
버린 대안: 워커가 manifest URL을 scope 상대로 풀게 바꾸기. 계약 차이는
없지만 워커 코드와 manifest 계약(root-relative)을 함께 바꿔야 해서
생성기 수정보다 크다.
위험·한계:
- base마다 manifest digest가 달라진다(의도된 결과).
- VITE_RUNTIME_CONFIG_URL이 빈 문자열이면 이제 base 기본값을 쓴다.
페이지(getBuildConfig)와 같은 규칙이다.
- 리뷰 완료 기준의 "e2e 도우미를 base 옵션으로 돌린 첫 설치가
RELOAD_TO_ENABLE"은 하지 않았다. e2e 도우미에 base 옵션과 /app/
서빙이 필요하고, 실제 브라우저 install은 이번에 확인하지 않았다.
검증:
- RED: collectStaticAssets 단위 테스트(basePath "/app/")가 수정 전
`/assets/index-abcdefgh.js`를 받아 실패(1 failed | 9 passed).
service-worker-build-output.test.ts의 새 테스트(SW ACTIVE·
production·/app/ 실제 빌드)가 생성기만 되돌리면 URL 목록에서,
SW 설정만 되돌리면 `Received: "/config.json"`으로 각각 실패.
- GREEN: 두 테스트 통과. `node scripts/run-vitest.ts run
tests/unit/service-worker tests/unit/sub-path-deployment.test.ts
tests/unit/hosting-probe.test.ts tests/unit/web-push` -> 12파일
171건 통과(빌드 테스트는 flock 안에서).
- `npx tsc` node·test·app·service-worker -> 오류 0.
- `npx eslint <바꾼 파일 4개> --max-warnings=0` -> 통과.
- `node scripts/check-adapter-inventory.ts` -> PASS.
- 미실행: SW e2e(Playwright).
Refs: docs/reviews/2026-09-29-maturity-review/06-adapters-service-worker-push.md 06-02, docs/reviews/2026-11-01-maturity-review/06-adapters-service-worker-push.md 2.1절
211 lines
7.0 KiB
TypeScript
211 lines
7.0 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import {
|
|
CACHEABLE_ASSET_CONTENT_TYPES,
|
|
decodeStaticAssetManifest,
|
|
isCanonicalStaticAssetUrl,
|
|
} from "../../src/contracts/service-worker-static-manifest.ts";
|
|
|
|
/**
|
|
* SW-RR-03. The build generator and the shared runtime decoder must agree on
|
|
* exactly which asset kinds exist. A generator that emits `.mjs` or `.png` while
|
|
* the decoder refuses them turns a correct build into a runtime contract
|
|
* failure, and the reverse admits a kind no build produces.
|
|
*/
|
|
describe("SW-RR-03 one authoritative cacheable asset table", () => {
|
|
it("covers every extension the generator emits", async () => {
|
|
const generator = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
expect(generator).toBeDefined();
|
|
for (const extension of [
|
|
".js",
|
|
".mjs",
|
|
".css",
|
|
".woff2",
|
|
".svg",
|
|
".png",
|
|
".webp",
|
|
]) {
|
|
expect(CACHEABLE_ASSET_CONTENT_TYPES[extension]).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it("decodes a manifest row for every table entry", () => {
|
|
const assets = Object.entries(CACHEABLE_ASSET_CONTENT_TYPES).map(
|
|
([extension, contentType], index) => ({
|
|
url: `/assets/name-abcdefgh${index}${extension}`,
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 16,
|
|
contentType,
|
|
}),
|
|
);
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
assets,
|
|
setDigest: `sha256:${"b".repeat(64)}`,
|
|
});
|
|
expect(decoded).toMatchObject({ ok: true });
|
|
});
|
|
|
|
it("refuses a row whose extension is not in the table", () => {
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
setDigest: `sha256:${"b".repeat(64)}`,
|
|
assets: [
|
|
{
|
|
url: "/assets/control-abcdefgh.json",
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 16,
|
|
contentType: "application/json",
|
|
},
|
|
],
|
|
});
|
|
expect(decoded.ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* SW-02. Sharing only the extension table left the generator and the decoder
|
|
* with different path grammars: the generator emitted a URL for a directory
|
|
* containing an `@` or a space, and the decoder then refused the manifest it
|
|
* had just produced, failing the release build at install time.
|
|
*/
|
|
describe("SW-02 the generator and the decoder share one path grammar", () => {
|
|
// 00-03. The dist fixtures used to stay in /tmp.
|
|
const temporaryRoots: string[] = [];
|
|
afterEach(async () => {
|
|
await Promise.all(
|
|
temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
|
);
|
|
});
|
|
|
|
async function distWith(
|
|
files: Readonly<Record<string, string>>,
|
|
): Promise<string> {
|
|
const root = await mkdtemp(path.join(tmpdir(), "sw-assets-"));
|
|
temporaryRoots.push(root);
|
|
for (const [relative, content] of Object.entries(files)) {
|
|
const absolute = path.join(root, relative);
|
|
await mkdir(path.dirname(absolute), { recursive: true });
|
|
await writeFile(absolute, content);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
it("emits a manifest the runtime decoder accepts for every table entry", async () => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const files: Record<string, string> = {};
|
|
for (const [index, extension] of Object.keys(
|
|
CACHEABLE_ASSET_CONTENT_TYPES,
|
|
).entries()) {
|
|
files[`assets/name-abcdefg${index}${extension}`] = `content-${index}`;
|
|
}
|
|
files["assets/nested/deep/name-abcdefgz.js"] = "nested";
|
|
const root = await distWith(files);
|
|
|
|
const manifest = await collectStaticAssets(root, "build-1", "release-1");
|
|
|
|
expect(manifest.assets.length).toBe(
|
|
Object.keys(CACHEABLE_ASSET_CONTENT_TYPES).length + 1,
|
|
);
|
|
expect(decodeStaticAssetManifest(manifest)).toMatchObject({ ok: true });
|
|
for (const asset of manifest.assets) {
|
|
expect(isCanonicalStaticAssetUrl(asset.url)).toBe(true);
|
|
}
|
|
});
|
|
|
|
// 0929/06-02. dist is served under the router base path; a root URL made
|
|
// the worker's install request /assets/... and fail under /app/.
|
|
it("puts every asset URL under the router base path", async () => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const root = await distWith({
|
|
"assets/index-abcdefgh.js": "entry",
|
|
"assets/nested/chunk-abcdefgz.css": "style",
|
|
});
|
|
|
|
const manifest = await collectStaticAssets(
|
|
root,
|
|
"build-1",
|
|
"release-1",
|
|
"/app/",
|
|
);
|
|
|
|
expect(manifest.assets.map((asset) => asset.url)).toEqual([
|
|
"/app/assets/index-abcdefgh.js",
|
|
"/app/assets/nested/chunk-abcdefgz.css",
|
|
]);
|
|
expect(decodeStaticAssetManifest(manifest)).toMatchObject({ ok: true });
|
|
});
|
|
|
|
it.each([
|
|
{ label: "an at sign", name: "bad@name-abcdefgh.js" },
|
|
{ label: "a space", name: "bad name-abcdefgh.js" },
|
|
{ label: "a percent escape", name: "bad%20name-abcdefgh.js" },
|
|
{ label: "a hash", name: "bad#name-abcdefgh.js" },
|
|
])(
|
|
"stops the build rather than emitting a path the decoder refuses ($label)",
|
|
async ({ name }) => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const root = await distWith({
|
|
"assets/good-abcdefgh.js": "ok",
|
|
[`assets/${name}`]: "bad",
|
|
});
|
|
|
|
await expect(
|
|
collectStaticAssets(root, "build-1", "release-1"),
|
|
).rejects.toThrow(/not canonical/u);
|
|
},
|
|
);
|
|
|
|
it("agrees with the decoder on the whole path policy table", () => {
|
|
const cases: readonly (readonly [string, boolean])[] = [
|
|
["/assets/name-abcdefgh.js", true],
|
|
["/assets/nested/name-abcdefgh.js", true],
|
|
["/assets/bad@name-abcdefgh.js", false],
|
|
["/assets/bad name-abcdefgh.js", false],
|
|
["/assets/bad%20name-abcdefgh.js", false],
|
|
["/assets/bad#name-abcdefgh.js", false],
|
|
["/assets/../name-abcdefgh.js", false],
|
|
["/assets/./name-abcdefgh.js", false],
|
|
["assets/name-abcdefgh.js", false],
|
|
["/assets/\\name-abcdefgh.js", false],
|
|
["/assets/naïve-abcdefgh.js", false],
|
|
];
|
|
for (const [url, canonical] of cases) {
|
|
expect(isCanonicalStaticAssetUrl(url), url).toBe(canonical);
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
setDigest: `sha256:${"a".repeat(64)}`,
|
|
assets: [
|
|
{
|
|
url,
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 4,
|
|
contentType: "text/javascript",
|
|
},
|
|
],
|
|
});
|
|
// The decoder still owns the digest check, so a canonical path may fail
|
|
// for other reasons; a non-canonical one must always fail.
|
|
if (!canonical) expect(decoded.ok, url).toBe(false);
|
|
}
|
|
});
|
|
});
|