fix(presentation): keep preferences above the scope fence, dispose routers (R-8.3)
LocaleProvider and ThemeProvider lived inside AppRouter, below the server-state scope fence (and below QueryClientProvider keyed by generation). A session change (FENCED -> READY) unmounted and remounted them, so their live state was thrown away and re-read from storage. The browser router was created in useMemo and never disposed, so every remount (and StrictMode's double useMemo) left a popstate listener behind. - New presentation/providers/preference-providers.tsx: persisted LocaleProvider + ThemeProvider (the wiring T45 added to AppRouter). - bootstrap/runtime-application.tsx: ApplicationProvider > PreferenceProviders > ServerStateGenerationProvider > AppRouter, so the fence only remounts session-scoped UI. - AppRouter no longer mounts Locale/Theme. It creates the router in the effect that disposes it: a disposed router never listens to history again, so useMemo + cleanup would break back/forward under StrictMode's replayed mount. - Tests that mount AppRouter directly now wrap it in PreferenceProviders (router, chunk-recovery-boundary, app-shell, product-feature-switch, locale-platform, reference-feature reference-page). Verified: RED "keeps locale state across a session scope fence and disposes routers" (RuntimeApplication, demo sign-in, storage cleared after choosing en-US) came back in Korean before and passes after; with the providers hoisted but no dispose it fails on popstate listeners (4 added, 0 removed). 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
e5ef6ba236
commit
ddf74ed6e9
@@ -1,6 +1,7 @@
|
||||
import { StrictMode, useEffect } from "react";
|
||||
|
||||
import { ApplicationProvider } from "../presentation/providers/application-provider.tsx";
|
||||
import { PreferenceProviders } from "../presentation/providers/preference-providers.tsx";
|
||||
import { ServerStateGenerationProvider } from "../presentation/adapters/query/server-state-generation-provider.tsx";
|
||||
import { AppRouter } from "../presentation/routes/app-router.tsx";
|
||||
import type { RuntimeComposition } from "./create-runtime-composition.ts";
|
||||
@@ -28,28 +29,32 @@ export function RuntimeApplication({
|
||||
}: Readonly<{ composition: RuntimeComposition }>) {
|
||||
return (
|
||||
<StrictMode>
|
||||
<ServerStateGenerationProvider
|
||||
store={composition.infrastructure.serverStateGeneration}
|
||||
scope={composition.infrastructure.serverStateScope}
|
||||
mutationIntentFactory={
|
||||
composition.infrastructure.mutationIntentFactory
|
||||
}
|
||||
transitionFallback={
|
||||
<div
|
||||
aria-busy="true"
|
||||
className="state-surface state-surface--loading"
|
||||
data-scope-state="scope-transition"
|
||||
/>
|
||||
}
|
||||
>
|
||||
<ApplicationProvider application={composition.application}>
|
||||
<PostMountRuntimeStarter composition={composition} />
|
||||
<AppRouter
|
||||
basename={composition.config.build.routerBasePath}
|
||||
buildId={composition.release.buildId}
|
||||
/>
|
||||
</ApplicationProvider>
|
||||
</ServerStateGenerationProvider>
|
||||
{/* Preferences sit above the scope fence: a session change remounts
|
||||
everything below it, and locale/theme must survive that. */}
|
||||
<ApplicationProvider application={composition.application}>
|
||||
<PreferenceProviders>
|
||||
<ServerStateGenerationProvider
|
||||
store={composition.infrastructure.serverStateGeneration}
|
||||
scope={composition.infrastructure.serverStateScope}
|
||||
mutationIntentFactory={
|
||||
composition.infrastructure.mutationIntentFactory
|
||||
}
|
||||
transitionFallback={
|
||||
<div
|
||||
aria-busy="true"
|
||||
className="state-surface state-surface--loading"
|
||||
data-scope-state="scope-transition"
|
||||
/>
|
||||
}
|
||||
>
|
||||
<PostMountRuntimeStarter composition={composition} />
|
||||
<AppRouter
|
||||
basename={composition.config.build.routerBasePath}
|
||||
buildId={composition.release.buildId}
|
||||
/>
|
||||
</ServerStateGenerationProvider>
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>
|
||||
</StrictMode>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
import { LocaleProvider, normalizeLocale } from "../i18n/index.ts";
|
||||
import { useApplicationPreferences } from "./application-provider.tsx";
|
||||
import { ThemeProvider } from "./theme-provider.tsx";
|
||||
|
||||
/**
|
||||
* User preferences outlive a session. Mount this above the server-state scope
|
||||
* fence so a session change (FENCED → READY) does not reset locale or theme.
|
||||
*/
|
||||
export function PreferenceProviders({
|
||||
children,
|
||||
}: Readonly<{ children: ReactNode }>) {
|
||||
const preferences = useApplicationPreferences();
|
||||
return (
|
||||
<LocaleProvider
|
||||
initialLocale={normalizeLocale(preferences.getLocale() ?? "ko-KR")}
|
||||
onLocaleChange={preferences.setLocale}
|
||||
>
|
||||
<ThemeProvider>{children}</ThemeProvider>
|
||||
</LocaleProvider>
|
||||
);
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
type ReactNode,
|
||||
Suspense,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
@@ -39,16 +38,14 @@ import { ChunkRecoveryBoundary } 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";
|
||||
import { LocaleProvider, normalizeLocale, useLocale } from "../i18n/index.ts";
|
||||
import { useLocale } from "../i18n/index.ts";
|
||||
import { AppShell } from "../layouts/app-shell.tsx";
|
||||
import {
|
||||
useApplicationDiagnostics,
|
||||
useApplicationPreferences,
|
||||
useApplicationRecovery,
|
||||
useApplicationRuntime,
|
||||
} from "../providers/application-provider.tsx";
|
||||
import { SessionProvider, useSession } from "../providers/session-provider.tsx";
|
||||
import { ThemeProvider } from "../providers/theme-provider.tsx";
|
||||
import {
|
||||
createRedirectLoopGuard,
|
||||
decideRouteAccess,
|
||||
@@ -498,28 +495,30 @@ function createRegisteredRoutes(buildId: string): RouteObject[] {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Locale and theme come from `PreferenceProviders` above this component, which
|
||||
* the server-state scope fence may remount on a session change.
|
||||
*/
|
||||
export function AppRouter({
|
||||
basename = "/",
|
||||
buildId = "local-build",
|
||||
}: Readonly<{ basename?: string; buildId?: string }>) {
|
||||
const preferences = useApplicationPreferences();
|
||||
const router = useMemo(
|
||||
() =>
|
||||
createBrowserRouter(createRegisteredRoutes(buildId), {
|
||||
basename,
|
||||
}),
|
||||
[basename, buildId],
|
||||
);
|
||||
const [router, setRouter] = useState<ReturnType<
|
||||
typeof createBrowserRouter
|
||||
> | null>(null);
|
||||
// Created in the effect that disposes it: a disposed router never listens
|
||||
// to history again, so StrictMode's replayed mount needs a fresh one, and a
|
||||
// remount must not leave the old router's popstate listener behind.
|
||||
useEffect(() => {
|
||||
const created = createBrowserRouter(createRegisteredRoutes(buildId), {
|
||||
basename,
|
||||
});
|
||||
setRouter(created);
|
||||
return () => created.dispose();
|
||||
}, [basename, buildId]);
|
||||
return (
|
||||
<LocaleProvider
|
||||
initialLocale={normalizeLocale(preferences.getLocale() ?? "ko-KR")}
|
||||
onLocaleChange={preferences.setLocale}
|
||||
>
|
||||
<ThemeProvider>
|
||||
<SessionProvider>
|
||||
<RouterProvider router={router} />
|
||||
</SessionProvider>
|
||||
</ThemeProvider>
|
||||
</LocaleProvider>
|
||||
<SessionProvider>
|
||||
{router ? <RouterProvider router={router} /> : null}
|
||||
</SessionProvider>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import userEvent from "@testing-library/user-event";
|
||||
import { afterEach, beforeEach, describe, expect, it } 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";
|
||||
|
||||
@@ -69,7 +70,9 @@ function renderShell() {
|
||||
window.history.pushState({}, "", "/");
|
||||
return render(
|
||||
<ApplicationProvider application={createTestApplication()}>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} 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 { 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";
|
||||
|
||||
@@ -148,7 +149,9 @@ describe("chunk recovery boundary classification", () => {
|
||||
window.history.pushState({}, "", "/examples/states");
|
||||
render(
|
||||
<ApplicationProvider application={createTestApplication()}>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
type SupportedLocale,
|
||||
} from "../../src/presentation/i18n/index.ts";
|
||||
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";
|
||||
|
||||
@@ -142,7 +143,9 @@ describe("persisted locale preference", () => {
|
||||
const mount = () =>
|
||||
render(
|
||||
<ApplicationProvider application={application}>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
window.history.pushState({}, "", "/missing");
|
||||
|
||||
@@ -7,6 +7,7 @@ import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-
|
||||
import { ROUTE_REGISTRY } from "../../src/features/installed-feature-contracts.ts";
|
||||
import { INSTALLED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts";
|
||||
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";
|
||||
import { createProductFeaturesStub } from "../helpers/runtime-capabilities-stub.ts";
|
||||
@@ -38,7 +39,9 @@ function renderAt(path: string, disabled: boolean) {
|
||||
: {}),
|
||||
})}
|
||||
>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-
|
||||
import type { ApplicationOutputPorts } from "../../src/application/create-application.ts";
|
||||
import { INSTALLED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts";
|
||||
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";
|
||||
import { createProductFeaturesStub } from "../helpers/runtime-capabilities-stub.ts";
|
||||
@@ -24,7 +25,9 @@ function renderRouter(
|
||||
...(productFeatures ? { productFeatures } : {}),
|
||||
})}
|
||||
>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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 { createRuntimeComposition } from "../../src/bootstrap/create-runtime-composition.ts";
|
||||
@@ -71,4 +72,53 @@ describe("production runtime application tree", () => {
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
expect(composition).not.toHaveProperty("ports");
|
||||
});
|
||||
|
||||
it("keeps locale state across a session scope fence and disposes routers", async () => {
|
||||
const user = userEvent.setup();
|
||||
const fetcher = vi.fn(async (input: RequestInfo | URL) => {
|
||||
const url =
|
||||
typeof input === "string"
|
||||
? input
|
||||
: input instanceof URL
|
||||
? input.href
|
||||
: input.url;
|
||||
return Response.json(
|
||||
url.includes("release-manifest") ? releaseManifest : runtimeConfig,
|
||||
);
|
||||
});
|
||||
window.localStorage.clear();
|
||||
const composition = await createRuntimeComposition({
|
||||
fetcher,
|
||||
host: { localStorage: window.localStorage },
|
||||
});
|
||||
// Spies call through; each router's history listens to popstate.
|
||||
const add = vi.spyOn(window, "addEventListener");
|
||||
const remove = vi.spyOn(window, "removeEventListener");
|
||||
const popstateCalls = (spy: typeof add | typeof remove) =>
|
||||
spy.mock.calls.filter(([type]) => type === "popstate").length;
|
||||
|
||||
try {
|
||||
window.history.pushState({}, "", "/missing");
|
||||
const app = render(<RuntimeApplication composition={composition} />);
|
||||
await user.selectOptions(await screen.findByLabelText("언어"), "en-US");
|
||||
// Only the live provider holds the choice from here on; a remount would
|
||||
// start over from empty storage.
|
||||
window.localStorage.clear();
|
||||
|
||||
// Demo sign-in changes the session: the scope goes FENCED, then READY.
|
||||
await user.click(screen.getByRole("button", { name: "Sign in" }));
|
||||
expect(
|
||||
await screen.findByRole("button", { name: "Sign out" }),
|
||||
).toBeVisible();
|
||||
expect(screen.getByLabelText("Language")).toHaveValue("en-US");
|
||||
|
||||
app.unmount();
|
||||
expect(popstateCalls(add)).toBeGreaterThan(0);
|
||||
expect(popstateCalls(remove)).toBe(popstateCalls(add));
|
||||
} finally {
|
||||
add.mockRestore();
|
||||
remove.mockRestore();
|
||||
await composition.dispose();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,6 +29,7 @@ import { MutationIntentProvider } from "../../../src/presentation/adapters/query
|
||||
import { ServerStateScopeProvider } from "../../../src/presentation/adapters/query/server-state-scope-provider.tsx";
|
||||
import { createServerStateScopeRuntime } from "../../../src/adapters/query-cache/server-state-scope-runtime.ts";
|
||||
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";
|
||||
|
||||
@@ -84,7 +85,9 @@ function renderReference(
|
||||
featureInputs: { [REFERENCE_FEATURE_ID]: input },
|
||||
})}
|
||||
>
|
||||
<AppRouter />
|
||||
<PreferenceProviders>
|
||||
<AppRouter />
|
||||
</PreferenceProviders>
|
||||
</ApplicationProvider>
|
||||
</QueryInvalidationProvider>
|
||||
</ServerStateScopeProvider>
|
||||
|
||||
Reference in New Issue
Block a user