fix(presentation): keep locale changes out of the route-change effect (R-8.4)
RouteLifecycle ran title, main focus, scroll-to-top and reportRouteChanged in one effect that depended on message/resolve, which the LocaleProvider recreates on every locale change. Changing the language therefore moved focus off the language select (main, then the h1), scrolled to the top and reported a route change, against the routing doc's "locale change re-renders the title and copy only" contract. The title now has its own effect (locale-dependent). Focus, scroll and telemetry run on pathname/search only; location.key is dropped, so a hash-only move (skip link, error summary #field) no longer re-runs them. Verified: RED "changes only the title and copy when the locale changes" failed before (focus moved off the locale select) and passes after; it also checks document.title and the route.changed count. check:architecture, check:types:app, check:types:test, lint, test:component 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
3b3765cc49
commit
938882dbd3
@@ -139,14 +139,17 @@ function RouteLifecycle({
|
||||
definition: RouteDefinition;
|
||||
buildId: string;
|
||||
}) {
|
||||
const location = useLocation();
|
||||
const { pathname, search } = useLocation();
|
||||
const { message, resolve } = useLocale();
|
||||
const diagnostics = useApplicationDiagnostics();
|
||||
// A locale change re-renders the title only; it is not a route change.
|
||||
useEffect(() => {
|
||||
document.title = message("route.documentTitle", {
|
||||
title: resolve(`route.${definition.routeId}.title`),
|
||||
appName: message("common.appName"),
|
||||
});
|
||||
}, [definition, message, resolve]);
|
||||
useEffect(() => {
|
||||
const main = document.getElementById("main-content");
|
||||
main?.focus({ preventScroll: true });
|
||||
try {
|
||||
@@ -160,15 +163,7 @@ function RouteLifecycle({
|
||||
routeId: definition.routeId,
|
||||
buildId,
|
||||
});
|
||||
}, [
|
||||
buildId,
|
||||
definition,
|
||||
diagnostics,
|
||||
location.key,
|
||||
location.pathname,
|
||||
message,
|
||||
resolve,
|
||||
]);
|
||||
}, [buildId, definition, diagnostics, pathname, search]);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,22 @@
|
||||
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-session-adapter.ts";
|
||||
import type { ApplicationOutputPorts } from "../../src/application/create-application.ts";
|
||||
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";
|
||||
|
||||
function renderRouter() {
|
||||
function renderRouter(
|
||||
diagnostics: ApplicationOutputPorts["diagnostics"] = { record: () => {} },
|
||||
) {
|
||||
return render(
|
||||
<ApplicationProvider
|
||||
application={createTestApplication({
|
||||
session: createAnonymousSessionAdapter(),
|
||||
diagnostics,
|
||||
})}
|
||||
>
|
||||
<AppRouter />
|
||||
@@ -21,6 +25,12 @@ function renderRouter() {
|
||||
);
|
||||
}
|
||||
|
||||
function routeChangeReports(record: ReturnType<typeof vi.fn>) {
|
||||
return record.mock.calls.filter(
|
||||
([event]) => (event as { eventId: string }).eventId === "route.changed",
|
||||
).length;
|
||||
}
|
||||
|
||||
describe("generic application router", () => {
|
||||
it("reaches the platform overview from the home starter actions", async () => {
|
||||
const user = userEvent.setup();
|
||||
@@ -81,6 +91,38 @@ describe("generic application router", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("changes only the title and copy when the locale changes", async () => {
|
||||
// docs/architecture/routing-pages-and-patterns.md §9: a locale change
|
||||
// re-renders the document title and copy; it is not a route change.
|
||||
const user = userEvent.setup();
|
||||
const record = vi.fn();
|
||||
window.history.pushState({}, "", "/missing");
|
||||
renderRouter({ record });
|
||||
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole("heading", {
|
||||
name: "페이지를 찾을 수 없습니다.",
|
||||
level: 1,
|
||||
}),
|
||||
).toHaveFocus(),
|
||||
);
|
||||
const reportsBefore = routeChangeReports(record);
|
||||
expect(reportsBefore).toBeGreaterThan(0);
|
||||
|
||||
const localeSelect = screen.getByLabelText("언어");
|
||||
await user.selectOptions(localeSelect, "en-US");
|
||||
|
||||
await waitFor(() =>
|
||||
expect(document.title).toBe("Page not found · Frontend Skeleton"),
|
||||
);
|
||||
expect(
|
||||
screen.getByRole("heading", { name: "Page not found.", level: 1 }),
|
||||
).toBeVisible();
|
||||
expect(localeSelect).toHaveFocus();
|
||||
expect(routeChangeReports(record)).toBe(reportsBefore);
|
||||
});
|
||||
|
||||
it("focuses the route heading again when the lazy chunk is already cached", async () => {
|
||||
// §9.7. The first visit resolves the route module asynchronously, so the
|
||||
// router lifecycle and the page header commit separately. A later visit
|
||||
|
||||
Reference in New Issue
Block a user