문제: 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절
108 lines
3.7 KiB
TypeScript
108 lines
3.7 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { defineConfig } from "vite";
|
|
|
|
import { getBuildConfig } from "./src/contracts/env.ts";
|
|
import {
|
|
SERVICE_WORKER_CACHE_SCHEMA_VERSION,
|
|
SERVICE_WORKER_PROTOCOL_VERSION,
|
|
SERVICE_WORKER_SCRIPT_PATH,
|
|
} from "./src/contracts/service-worker.ts";
|
|
import { INSTALLED_RUNTIME_CAPABILITIES } from "./src/features/installed-runtime-capabilities.ts";
|
|
import { resolveServiceWorkerBuildInput } from "./scripts/lib/service-worker-build-input.ts";
|
|
|
|
/**
|
|
* §17.2.2 step 5. The Service Worker build.
|
|
*
|
|
* Runs only when the static selection is `ACTIVE`, after the app build has
|
|
* completed and the hashed asset list has been generated. `emptyOutDir` is
|
|
* false so it adds `service-worker.js` beside the already-built app rather than
|
|
* replacing it.
|
|
*/
|
|
|
|
const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker;
|
|
|
|
// Relative to this file: Vite's default `bundle` config loader runs the config
|
|
// from `node_modules/.vite-temp/`, so a bare `import("./.generated/...")` would
|
|
// resolve there. Every loader keeps `import.meta.url` pointing at this file.
|
|
async function readGenerated<T>(path: string, exportName: string): Promise<T> {
|
|
try {
|
|
const module = (await import(new URL(path, import.meta.url).href)) as Record<
|
|
string,
|
|
unknown
|
|
>;
|
|
if (!(exportName in module)) {
|
|
throw new TypeError(`Generated module does not export ${exportName}.`);
|
|
}
|
|
return module[exportName] as T;
|
|
} catch (error) {
|
|
throw new TypeError(`Required generated module is unavailable: ${path}`, {
|
|
cause: error,
|
|
});
|
|
}
|
|
}
|
|
|
|
const assets = await readGenerated(
|
|
"./.generated/frontend-runtime/service-worker-assets.ts",
|
|
"SERVICE_WORKER_ASSETS",
|
|
);
|
|
const contractSet = await readGenerated<{ setDigest?: string }>(
|
|
"./.generated/frontend-runtime/contract-set.ts",
|
|
"BUILD_CONTRACT_SET",
|
|
);
|
|
// 06-10 / F-8. The profile document `scripts/generate-runtime-config.ts` wrote
|
|
// in step 4, not `public/config.json`, which is always the local profile.
|
|
const runtimeConfig = JSON.parse(
|
|
await readFile("dist/config.json", "utf8"),
|
|
) as Record<string, string>;
|
|
|
|
const buildId = process.env.VITE_BUILD_ID ?? "local-build";
|
|
const releaseId = process.env.RELEASE_ID ?? "local-release";
|
|
const buildInput = resolveServiceWorkerBuildInput({
|
|
selection,
|
|
assets,
|
|
contractSet,
|
|
runtimeConfig,
|
|
appProfile: process.env.APP_PROFILE ?? "local",
|
|
buildId,
|
|
releaseId,
|
|
});
|
|
|
|
export default defineConfig({
|
|
// 0929/06-01. `dist` already holds the app build, `public/` included. Copying
|
|
// `public/` again would put the local `public/config.json` back over the
|
|
// profile document step 4 wrote.
|
|
publicDir: false,
|
|
build: {
|
|
// §17.2.2: exactly one input, exactly one output, no additional chunk.
|
|
emptyOutDir: false,
|
|
sourcemap: false,
|
|
rollupOptions: {
|
|
input: "src/adapters/service-worker/service-worker-entry.ts",
|
|
output: {
|
|
format: "es",
|
|
inlineDynamicImports: true,
|
|
entryFileNames: SERVICE_WORKER_SCRIPT_PATH,
|
|
},
|
|
},
|
|
},
|
|
define: {
|
|
__CA_SERVICE_WORKER_BUILD_INFO__: JSON.stringify({
|
|
serviceWorkerProtocolVersion: SERVICE_WORKER_PROTOCOL_VERSION,
|
|
cacheSchemaVersion: SERVICE_WORKER_CACHE_SCHEMA_VERSION,
|
|
buildId,
|
|
releaseId,
|
|
contractSetDigest: buildInput.contractSetDigest,
|
|
staticAssetSetDigest: buildInput.assets.setDigest,
|
|
}),
|
|
__CA_SERVICE_WORKER_ASSETS__: JSON.stringify(buildInput.assets),
|
|
__CA_SERVICE_WORKER_HANDLERS__: JSON.stringify(buildInput.handlers),
|
|
// 0929/06-02. The page's own default, which follows the router base path.
|
|
__CA_RUNTIME_CONFIG_URL__: JSON.stringify(
|
|
getBuildConfig(process.env).runtimeConfigUrl,
|
|
),
|
|
__CA_RELEASE_MANIFEST_URL__: JSON.stringify(
|
|
buildInput.releaseManifestUrl,
|
|
),
|
|
},
|
|
});
|