diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx index cf29ef07..c21fa07d 100644 --- a/src/presentation/routes/app-router.tsx +++ b/src/presentation/routes/app-router.tsx @@ -20,6 +20,7 @@ import { import { getRoute, ROUTE_FEATURE_OWNER, + ROUTE_REGISTRY, } from "../../features/installed-feature-contracts.ts"; import { PLATFORM_ROUTE_REGISTRY, @@ -34,7 +35,10 @@ import { FeatureBoundary, RouteBoundary, } from "../boundaries/render-error-boundary.tsx"; -import { ChunkRecoveryBoundary } from "../boundaries/chunk-recovery-boundary.tsx"; +import { + ChunkRecoveryBoundary, + isChunkLoadFailure, +} from "../boundaries/chunk-recovery-boundary.tsx"; import { ContextErrorBoundary } from "../../platform/errors/context-error-boundary.tsx"; import { FeatureErrorBoundary } from "../../platform/errors/feature-error-boundary.tsx"; import { Button, PageHeader } from "../design-system/index.ts"; @@ -379,25 +383,42 @@ function isContextRouteHandle(value: unknown): value is ContextRouteHandle { ); } +/** The registered route a URL names, to key chunk recovery on its chunk. */ +function routeChunkId(pathname: string): string | undefined { + const routes = Object.values(ROUTE_REGISTRY).map((definition) => ({ + id: definition.routeId, + path: definition.path, + })); + const routeId = matchRoutes(routes, pathname)?.at(-1)?.route.id; + return routeId === undefined ? undefined : getRoute(routeId).chunkId; +} + +function Rethrow({ error }: Readonly<{ error: unknown }>): never { + throw error; +} + function ContextRouteGateway({ routeModule, buildId, }: Readonly<{ routeModule: ContextRouteModule; buildId: string }>) { const location = useLocation(); const { message } = useLocale(); + const recovery = useApplicationRecovery(); const [routes, setRoutes] = useState(null); - const [failed, setFailed] = useState(false); + const [failure, setFailure] = useState | null>( + null, + ); useEffect(() => { let active = true; setRoutes(null); - setFailed(false); + setFailure(null); void routeModule.loadRoutes().then( (loaded) => { if (active) setRoutes(loaded); }, - () => { - if (active) setFailed(true); + (error: unknown) => { + if (active) setFailure({ error }); }, ); return () => { @@ -416,7 +437,20 @@ function ContextRouteGateway({ ); - if (failed) return ; + if (failure) { + // A routes module that failed to download after a deploy gets the same + // reload-once recovery as a route page chunk. + return isChunkLoadFailure(failure.error) ? ( + + + + ) : ( + + ); + } if (routes === null) { return (
@@ -480,7 +514,13 @@ function createRegisteredRoutes(buildId: string): RouteObject[] { id: `CONTEXT:${routeModule.id}`, path: routeModule.path, element: ( - + // Keyed: React Router reuses the instance across sibling contexts, + // and the previous context's routes must never match the new URL. + ), }), ); diff --git a/tests/component/context-route-gateway.test.tsx b/tests/component/context-route-gateway.test.tsx new file mode 100644 index 00000000..cb5a5d23 --- /dev/null +++ b/tests/component/context-route-gateway.test.tsx @@ -0,0 +1,129 @@ +// @vitest-environment jsdom + +import { act, 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"; + +/** + * Three bounded contexts cut from the real reference routes, so canonical + * redirects and access rules stay valid: A owns the list, B the status page, + * and the third one's routes module fails to download. + */ +vi.mock("../../src/apps/template-app.ts", async () => { + const { FEATURE_CONTEXT_ROUTES } = await import( + "../../src/features/reference-feature/context-routes.ts" + ); + const only = (id: string) => async () => + FEATURE_CONTEXT_ROUTES.filter((route) => route.id === id); + return { + TEMPLATE_APP_MODULE: { + id: "two-context-app", + budget: { maxContexts: 3 }, + contexts: [ + { + id: "context-a", + route: { + path: "examples/reference-resources/*", + loadRoutes: only("REFERENCE_RESOURCE_LIST"), + }, + }, + { + id: "context-b", + route: { + path: "examples/reference-resources/status/*", + loadRoutes: only("REFERENCE_RESOURCE_STATUS"), + }, + }, + { + id: "context-offline", + route: { + path: "examples/reference-resources/new/*", + loadRoutes: async () => { + throw new TypeError( + "Failed to fetch dynamically imported module: /assets/context-routes.js", + ); + }, + }, + }, + ], + }, + }; +}); + +function navigateByHistory(url: string) { + act(() => { + window.history.pushState({}, "", url); + window.dispatchEvent(new PopStateEvent("popstate")); + }); +} + +function reportedRoutes(record: ReturnType): string[] { + return record.mock.calls + .map(([event]) => event as { eventId: string; context: { route_id: string } }) + .filter((event) => event.eventId === "route.changed") + .map((event) => event.context.route_id); +} + +describe("context route gateway", () => { + it("never matches a new context's URL against the previous context's routes", async () => { + const record = vi.fn(); + window.history.pushState({}, "", "/examples/reference-resources"); + render( + + + + + , + ); + await waitFor(() => + expect(reportedRoutes(record)).toContain("REFERENCE_RESOURCE_LIST"), + ); + + navigateByHistory("/examples/reference-resources/status"); + + await waitFor(() => + expect(reportedRoutes(record)).toContain("REFERENCE_RESOURCE_STATUS"), + ); + expect(reportedRoutes(record)).not.toContain("NOT_FOUND"); + }); + + it("sends a failed routes-module download through chunk recovery", async () => { + const reload = vi.fn(); + const release = (buildId: string, releaseId: string) => ({ + buildId, + releaseId, + configSchemaVersion: "1", + apiContractVersion: "1", + assetManifestHash: "hash", + routeChunks: { "route-reference-resource-form": "assets/form.js" }, + }); + window.history.pushState({}, "", "/examples/reference-resources/new"); + render( + release("build-a", "release-a"), + refresh: async () => release("build-b", "release-b"), + }, + })} + > + + + + , + ); + + await waitFor(() => expect(reload).toHaveBeenCalledOnce()); + expect( + await screen.findByText("새 버전으로 한 번만 전환합니다."), + ).toBeVisible(); + }); +});