Files
DongHyeonka 4a43801859 perf(routes): context의 기능 실행부를 라우트 모듈과 함께 받기 시작한다
context에 처음 들어가면 라우트 모듈과 들어간 주소의 기능 실행부만 함께 받고,
그 뒤에야 같은 context의 나머지 기능 실행부를 받는다
(`src/presentation/routes/app-router.tsx` `ContextRouteGateway`). 그 전에는
라우트 로딩 표면이다. 그런데 그 기능 목록은 처음부터 정적으로 있다
(`routeModule.featureIds`). 그래서 기능 둘 이상을 한 context에 넣으면
(`create:feature --context`) 첫 진입의 요청 사슬이 "라우트 모듈 → 다른 기능
실행부 → 페이지 청크"로 한 묶음 늘어난다. 로그인 안내 화면도 그 뒤에 그려진다.
라우트를 갖지 않은 기능은 아예 받지 않아, 그 context의 페이지에서
`application.features.get`이 실패할 수 있었다.

고른 방법(리뷰의 (가)): 들어간 주소의 기능과 `routeModule.featureIds`를 라우트
모듈과 같은 `Promise.all`에서 받기 시작한다. 받은 라우트의 owner를 받는 둘째
단계는 남긴다. `features.load`가 기능마다 promise 하나를 나눠 이미 받는 중이면
왕복을 더하지 않고, context가 선언하지 않은 기능이 라우트를 가져도 그리기 전에
받는다. 버린 대안 (나): 들어간 기능만 기다려 그리고 나머지는 뒤에서 받기.
"`features.get`이 그 context의 모든 페이지에서 된다"는 보장을 라우트 단위로
줄이는 계약 변경이라 이번에 하지 않는다.

위험·한계: context에 선언만 하고 라우트가 없는 기능도 이제 첫 진입에 받는다
(받는 양은 같은 context의 기능 수에 비례, 전에도 라우트를 가진 기능은 모두
받았다). 그 기능의 실행부 적재가 실패하면 그 context 전체가 실패 화면으로
간다(전에는 라우트를 가진 기능의 실패만 그랬다). context의 페이지가 그 기능을
`features.get`으로 읽을 수 있다는 약속을 지키려면 그리기 전에 받아야 하므로
고른 동작이다. lab은 아직 기능 라우트를 재지 못해(1105/15-04, 그룹 C) 시간은 재지
않았다.

검증: `tests/component/context-feature-loading.test.tsx`(기능 둘인 context에서
라우트 모듈이 오기 전에 다른 기능의 실행부를 받기 시작한다)가 고치기 전에
실패하고 고친 뒤 통과한다(desktop, PR 본문).

Refs: 1112/06-01
2026-11-13 19:03:18 +09:00

116 lines
4.0 KiB
TypeScript

// @vitest-environment jsdom
import { render, screen, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx";
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
import { createTestApplication } from "../helpers/create-test-application.ts";
/**
* 1112/06-01. A context's features all load before its pages render, so that
* `application.features.get` works in every page of the context. The routes
* module and the features' runtime halves load side by side: a feature that
* does not own the entered URL must not wait for the routes module first.
*/
const gate = vi.hoisted(() => {
let releaseRoutes: () => void = () => {};
const routesLoaded = new Promise<void>((resolve) => {
releaseRoutes = resolve;
});
return {
routesLoaded,
releaseRoutes: () => releaseRoutes(),
peerRuntimeStarted: vi.fn(),
};
});
vi.mock("../../src/.generated/product-feature-index.ts", async () => {
const { FIXTURE_FEATURE_MODULE } = await import("../helpers/fixture-feature.tsx");
const { PEER_FEATURE_MODULE } = await peerModules();
return {
DECLARED_PRODUCT_FEATURE_IDS: ["fixture-feature", "peer-feature"],
PRODUCT_FEATURE_MODULES: [FIXTURE_FEATURE_MODULE, PEER_FEATURE_MODULE],
};
});
vi.mock("../../src/.generated/product-context-index.ts", async () => {
const { PEER_CONTEXT_MODULE } = await peerModules();
return { PRODUCT_CONTEXT_MODULES: [PEER_CONTEXT_MODULE] };
});
async function peerModules() {
const { defineFeatureModule } = await import("../../src/features/feature-module.ts");
const { defineBoundedContextModule } = await import(
"../../src/contexts/bounded-context-module.ts"
);
const { FIXTURE_CONTEXT_ROUTES, FIXTURE_FEATURE_MODULE } = await import(
"../helpers/fixture-feature.tsx"
);
const PEER_FEATURE_MODULE = defineFeatureModule({
id: "peer-feature",
contracts: Object.freeze({
featureId: "peer-feature",
routes: Object.freeze({}),
routeRuntimeContracts: Object.freeze({}),
invalidation: Object.freeze({ topics: [], namespaces: [], edges: [] }),
topicVersions: Object.freeze([]),
contractPackages: Object.freeze([]),
}),
routes: Object.freeze({}),
messages: Object.freeze({ "ko-KR": Object.freeze({}), "en-US": Object.freeze({}) }),
loadRuntime: async () =>
Object.freeze({
featureId: "peer-feature",
schemas: Object.freeze({}),
adapters: Object.freeze([]),
contractContributions: Object.freeze([]),
}),
});
const PEER_CONTEXT_MODULE = defineBoundedContextModule({
id: "fixture-context",
features: [FIXTURE_FEATURE_MODULE, PEER_FEATURE_MODULE],
budget: { maxFeatures: 2 },
route: {
path: "fixture/*",
loadRoutes: async () => {
await gate.routesLoaded;
return FIXTURE_CONTEXT_ROUTES;
},
},
});
return { PEER_FEATURE_MODULE, PEER_CONTEXT_MODULE };
}
describe("context feature loading", () => {
it("starts every feature of the context together with its routes module", async () => {
window.history.pushState({}, "", "/fixture/first");
render(
<ApplicationProvider
application={createTestApplication({
// The application loads a feature's runtime half through these.
featureLoaders: {
"peer-feature": async () => {
gate.peerRuntimeStarted();
return {};
},
},
})}
>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
// The routes module is still downloading.
await waitFor(() => expect(gate.peerRuntimeStarted).toHaveBeenCalledOnce());
gate.releaseRoutes();
expect(
await screen.findByRole("heading", { name: "Fixture first page", level: 1 }),
).toBeVisible();
expect(gate.peerRuntimeStarted).toHaveBeenCalledOnce();
});
});