fix(presentation): reset chunk recovery per route and offer a reload (R-8.1)

React Router 7 reuses the rendered-route instance between sibling routes,
so ChunkRecoveryBoundary kept its error/support state after the user left
the failed route and every later page showed "화면 자산을 복구하지 못했습니다"
until a manual browser reload. The boundary is now keyed by routeId, and
the support surface has a user-started "한 번 새로고침" button.

Verified: RED "clears a failed route chunk when the user moves to a sibling
route" (failure surface still rendered after navigating to /examples/ui)
and "offers a user-started reload on the failure surface" (no button) both
failed before the fix and pass after. check:architecture, check:types:app,
check:types:test, lint, test:component pass; test:unit passes except the
pre-existing removal-fixture test, which needs generated artifacts/ files
absent from a fresh worktree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 12:39:51 +09:00
co-authored by Claude Opus 5.5
parent 2fb07c857a
commit ad2b733ae9
3 changed files with 72 additions and 0 deletions
@@ -3,6 +3,7 @@ import {
type ErrorInfo,
type ReactNode,
} from "react";
import { Button } from "../design-system/index.ts";
import { useLocale } from "../i18n/index.ts";
type RecoveryResult =
@@ -110,6 +111,10 @@ function ChunkRecoverySurface({
<section className="ui-page" role="alert" data-recovery-reason={reason}>
<h1>{message("chunk.failure.title")}</h1>
<p>{message("chunk.failure.description")}</p>
{/* The automatic reload-once already declined; this one is the user's. */}
<Button onClick={() => window.location.reload()}>
{message("action.reloadOnce")}
</Button>
</section>
);
}
+3
View File
@@ -318,7 +318,10 @@ function RegisteredRoute({
<CanonicalRouteRedirect input={parsed.data} />
<RouteLifecycle definition={definition} buildId={buildId} />
<Suspense fallback={<RouteLoadingSurface definition={definition} />}>
{/* React Router reuses this instance across sibling routes; the key
keeps one route's failed chunk from latching onto the next. */}
<ChunkRecoveryBoundary
key={definition.routeId}
chunkId={definition.chunkId}
recover={recovery.recoverChunk}
>
@@ -1,6 +1,7 @@
// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import {
@@ -8,6 +9,19 @@ import {
isChunkLoadFailure,
} from "../../src/presentation/boundaries/chunk-recovery-boundary.tsx";
import { FeatureBoundary } from "../../src/presentation/boundaries/render-error-boundary.tsx";
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
import { createTestApplication } from "../helpers/create-test-application.ts";
// The states page stands in for a route chunk the network could not deliver:
// a rejected lazy module throws its rejection on every render, as this does.
vi.mock("../../src/presentation/examples/state-gallery-page.tsx", () => ({
default: function UnreachableChunk(): never {
throw new TypeError(
"Failed to fetch dynamically imported module: /assets/states.js",
);
},
}));
function ChunkDefect(): never {
throw new TypeError("Failed to fetch dynamically imported module");
@@ -65,4 +79,54 @@ describe("chunk recovery boundary classification", () => {
);
expect(recover).not.toHaveBeenCalled();
});
it("offers a user-started reload on the failure surface", async () => {
render(
<ChunkRecoveryBoundary
chunkId="route-home"
recover={async () => ({
action: "support" as const,
reason: "reload-already-attempted",
})}
>
<ChunkDefect />
</ChunkRecoveryBoundary>,
);
expect(
await screen.findByRole("button", { name: "한 번 새로고침" }),
).toBeVisible();
});
it("clears a failed route chunk when the user moves to a sibling route", async () => {
// React Router reuses the rendered-route instance between sibling routes,
// so a failure latched in one route's boundary would otherwise follow the
// user to every other page until a full reload.
const user = userEvent.setup();
window.history.pushState({}, "", "/examples/states");
render(
<ApplicationProvider application={createTestApplication()}>
<AppRouter />
</ApplicationProvider>,
);
expect(
await screen.findByRole("heading", {
name: "화면 자산을 복구하지 못했습니다.",
}),
).toBeVisible();
await user.click(screen.getAllByRole("link", { name: "UI 구성요소" })[0]!);
expect(
await screen.findByRole(
"heading",
{ name: "UI 구성요소", level: 1 },
{ timeout: 5000 },
),
).toBeVisible();
expect(
screen.queryByRole("heading", { name: "화면 자산을 복구하지 못했습니다." }),
).toBeNull();
});
});