fix(presentation): key the context gateway and recover its chunk failures (R-8.7)
(a) ContextRouteGateway had no key, so React Router reused one instance across sibling contexts. On a context A -> B move the first render still held A's routes, matched B's URL against them and mounted NOT_FOUND, which reported route.changed(NOT_FOUND) and moved title/focus once. The gateway element is now keyed by the context id. (b) A rejected loadRoutes() always showed RouteFailureSurface, so a routes module missing after a deploy never reached recoverChunk. A chunk load failure is now rethrown inside a ChunkRecoveryBoundary keyed on the chunk of the registered route the URL names (the routes module has no manifest entry of its own; recoverChunk needs one to decide reload-once). Other load errors still show RouteFailureSurface. Verified: new tests/component/context-route-gateway.test.tsx mocks the app with contexts cut from the real reference routes. RED "never matches a new context's URL against the previous context's routes" (NOT_FOUND reported) and "sends a failed routes-module download through chunk recovery" (navigation.reload never called after a release change) failed before and pass after. check:architecture, check:types:app, check:types:test, lint, test:component, test:reference-feature pass; test:unit passes except the pre-existing removal-fixture test (needs generated artifacts/ files absent from a fresh worktree). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ddf74ed6e9
commit
7022a1efd0
@@ -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<readonly RouteObject[] | null>(null);
|
||||
const [failed, setFailed] = useState(false);
|
||||
const [failure, setFailure] = useState<Readonly<{ error: unknown }> | 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({
|
||||
</ContextErrorBoundary>
|
||||
);
|
||||
|
||||
if (failed) return <RouteFailureSurface />;
|
||||
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) ? (
|
||||
<ChunkRecoveryBoundary
|
||||
chunkId={routeChunkId(location.pathname) ?? routeModule.id}
|
||||
recover={recovery.recoverChunk}
|
||||
>
|
||||
<Rethrow error={failure.error} />
|
||||
</ChunkRecoveryBoundary>
|
||||
) : (
|
||||
<RouteFailureSurface />
|
||||
);
|
||||
}
|
||||
if (routes === null) {
|
||||
return (
|
||||
<section className="ui-page route-loading" aria-live="polite" aria-busy="true">
|
||||
@@ -480,7 +514,13 @@ function createRegisteredRoutes(buildId: string): RouteObject[] {
|
||||
id: `CONTEXT:${routeModule.id}`,
|
||||
path: routeModule.path,
|
||||
element: (
|
||||
<ContextRouteGateway routeModule={routeModule} buildId={buildId} />
|
||||
// Keyed: React Router reuses the instance across sibling contexts,
|
||||
// and the previous context's routes must never match the new URL.
|
||||
<ContextRouteGateway
|
||||
key={routeModule.id}
|
||||
routeModule={routeModule}
|
||||
buildId={buildId}
|
||||
/>
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user