perf(build): react-dom을 자기 청크로 빼 빌드 id만 바뀐 배포의 캐시 손실을 줄인다
빌드 id는 부트 실패 화면이 보여 주므로 render-boot-failure 청크에 들어간다(getBuildConfig). react-dom과 scheduler가 같은 청크에 있어서 빌드 id만 다른 두 빌드에서 그 190,478 B 청크의 이름이 바뀌었다. 해시 자산은 오래 캐시되는데, 재방문 사용자는 코드가 같아도 배포마다 이만큼 다시 받았다. 단위 시험 build-environment-bundle이 이 청크가 바뀐다고 실패했다. 템플릿 af6d0a9f(1103/15-06)와 같은 방식이다. manualChunks에서 react-dom· scheduler를 react-dom 청크로 뺀다. 시험은 템플릿 새 판처럼 디자인 시스템 청크를 이름 대신 내용(ui-button--pending)으로 찾아 고정됐는지 보고, react-dom 청크가 고정이며 render-boot-failure가 아닌지 본다. 이전 시험은 빌드 id를 일부러 담는 render-boot-failure를 공용 청크로 잘못 골랐다. 위험·한계: 정규식은 pnpm의 node_modules 배치를 가정한다. 배치가 바뀌어 그룹이 비면 시험이 실패한다. boot(약 320 KB)는 빌드 id로 설정을 대조하므로 계속 이름이 바뀐다(템플릿과 같음). 검증: 빌드 id만 다른 두 빌드에서 이름이 바뀐 청크는 boot·index· render-boot-failure 셋이고, render-boot-failure는 12,138 B로 줄었다. react-dom 청크와 디자인 시스템 청크는 두 빌드에서 이름이 같다.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
@@ -67,16 +67,31 @@ describe("build variables in the bundle (13-06)", () => {
|
||||
|
||||
const second = await build({ ...DECLARED, VITE_BUILD_ID: "build-probe-2" });
|
||||
expect(second.run.status).toBe(0);
|
||||
// The shared application chunk, which Vite names after one of its modules:
|
||||
// `design-system-*` upstream. This product's route chunk split
|
||||
// (vite.config.ts) leaves `render-boot-failure-*` as that chunk here.
|
||||
const designSystem = async (outDir: string) =>
|
||||
(await readdir(path.join(outDir, "assets"))).filter((name) =>
|
||||
name.startsWith("render-boot-failure-"),
|
||||
);
|
||||
const firstChunks = await designSystem(first.outDir);
|
||||
// The design system, found by what it holds rather than by name: this
|
||||
// product's route chunk split (vite.config.ts) names chunks after route
|
||||
// modules, and upstream's `design-system-*` does not exist here.
|
||||
// (The previous check looked at `render-boot-failure-*`, which holds the
|
||||
// build id on purpose and can never keep its name.)
|
||||
const designSystem = ({ contents }: Awaited<ReturnType<typeof build>>) =>
|
||||
contents
|
||||
.filter(({ file, text }) =>
|
||||
file.endsWith(".js") && text.includes("ui-button--pending"),
|
||||
)
|
||||
.map(({ file }) => path.basename(file));
|
||||
const firstChunks = designSystem(first);
|
||||
expect(firstChunks).toHaveLength(1);
|
||||
expect(await designSystem(second.outDir)).toEqual(firstChunks);
|
||||
expect(designSystem(second)).toEqual(firstChunks);
|
||||
|
||||
// Template 1103/15-06. react-dom sat in the boot-failure chunk with the
|
||||
// build id, so every release renamed it and returning visitors downloaded
|
||||
// it again. Its own chunk keeps the name while only the build id changes.
|
||||
const reactDom = ({ contents }: Awaited<ReturnType<typeof build>>) =>
|
||||
contents
|
||||
.filter(({ file, text }) => file.endsWith(".js") && text.includes("__reactContainer$"))
|
||||
.map(({ file }) => path.basename(file));
|
||||
expect(reactDom(first)).toHaveLength(1);
|
||||
expect(reactDom(first)[0]).not.toMatch(/render-boot-failure/u);
|
||||
expect(reactDom(second)).toEqual(reactDom(first));
|
||||
}, 120_000);
|
||||
|
||||
it("refuses a VITE_* variable the build does not declare", async () => {
|
||||
|
||||
+14
-1
@@ -65,6 +65,10 @@ const techLogRouteChunks = Object.freeze({
|
||||
"/public/pages/public-not-found-page.tsx": "route-not-found",
|
||||
} as const);
|
||||
|
||||
// pnpm lays packages out as node_modules/.pnpm/<name>@<version>/node_modules/<name>.
|
||||
const REACT_DOM_MODULE =
|
||||
/[\\/]node_modules[\\/](?:\.pnpm[\\/][^\\/]+[\\/]node_modules[\\/])?(?:react-dom|scheduler)[\\/]/;
|
||||
|
||||
function routeChunkName(moduleId: string): string | undefined {
|
||||
const normalized = moduleId.replaceAll("\\", "/");
|
||||
const featurePath = "/src/features/tech-log/presentation";
|
||||
@@ -139,10 +143,19 @@ export default defineConfig(({ mode }) => {
|
||||
// 09-03. The entry now imports the app dynamically, so Vite's preload
|
||||
// helper would land in the entry chunk and every lazy chunk would
|
||||
// import the entry, whose hash changes with each build id.
|
||||
//
|
||||
// Template 1103/15-06 (af6d0a9f). react-dom and scheduler shared the
|
||||
// boot-failure chunk with the build id (getBuildConfig), so a release
|
||||
// that only changed the build id renamed that 190 KB chunk and
|
||||
// returning visitors downloaded it again. In a chunk of their own they
|
||||
// keep their name; only boot, the entry and the small boot-failure
|
||||
// screen, which hold or import the build id, are renamed.
|
||||
manualChunks: (id: string) =>
|
||||
id.includes("vite/preload-helper")
|
||||
? "preload-helper"
|
||||
: routeChunkName(id),
|
||||
: REACT_DOM_MODULE.test(id)
|
||||
? "react-dom"
|
||||
: routeChunkName(id),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user