diff --git a/src/bootstrap/runtime-application.tsx b/src/bootstrap/runtime-application.tsx
index 475eeed0..91739a04 100644
--- a/src/bootstrap/runtime-application.tsx
+++ b/src/bootstrap/runtime-application.tsx
@@ -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 (
-
- }
- >
-
-
-
-
-
+ {/* Preferences sit above the scope fence: a session change remounts
+ everything below it, and locale/theme must survive that. */}
+
+
+
+ }
+ >
+
+
+
+
+
);
}
diff --git a/src/presentation/providers/preference-providers.tsx b/src/presentation/providers/preference-providers.tsx
new file mode 100644
index 00000000..8b918dd2
--- /dev/null
+++ b/src/presentation/providers/preference-providers.tsx
@@ -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 (
+
+ {children}
+
+ );
+}
diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx
index 3c31dbb5..cf29ef07 100644
--- a/src/presentation/routes/app-router.tsx
+++ b/src/presentation/routes/app-router.tsx
@@ -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 | 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 (
-
-
-
-
-
-
-
+
+ {router ? : null}
+
);
}
diff --git a/tests/component/app-shell.test.tsx b/tests/component/app-shell.test.tsx
index b44fda39..9c9ff358 100644
--- a/tests/component/app-shell.test.tsx
+++ b/tests/component/app-shell.test.tsx
@@ -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(
-
+
+
+
,
);
}
diff --git a/tests/component/chunk-recovery-boundary.test.tsx b/tests/component/chunk-recovery-boundary.test.tsx
index 8c62e875..8efd76f5 100644
--- a/tests/component/chunk-recovery-boundary.test.tsx
+++ b/tests/component/chunk-recovery-boundary.test.tsx
@@ -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(
-
+
+
+
,
);
diff --git a/tests/component/locale-platform.test.tsx b/tests/component/locale-platform.test.tsx
index 843ad15c..f2733277 100644
--- a/tests/component/locale-platform.test.tsx
+++ b/tests/component/locale-platform.test.tsx
@@ -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(
-
+
+
+
,
);
window.history.pushState({}, "", "/missing");
diff --git a/tests/component/product-feature-switch.test.tsx b/tests/component/product-feature-switch.test.tsx
index 8c4f5ff6..10a51793 100644
--- a/tests/component/product-feature-switch.test.tsx
+++ b/tests/component/product-feature-switch.test.tsx
@@ -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) {
: {}),
})}
>
-
+
+
+
,
);
}
diff --git a/tests/component/router.test.tsx b/tests/component/router.test.tsx
index 46c41d99..161343fa 100644
--- a/tests/component/router.test.tsx
+++ b/tests/component/router.test.tsx
@@ -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 } : {}),
})}
>
-
+
+
+
,
);
}
diff --git a/tests/component/runtime-application.test.tsx b/tests/component/runtime-application.test.tsx
index 1da675a9..57957678 100644
--- a/tests/component/runtime-application.test.tsx
+++ b/tests/component/runtime-application.test.tsx
@@ -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();
+ 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();
+ }
+ });
});
diff --git a/tests/features/reference-feature/reference-page.test.tsx b/tests/features/reference-feature/reference-page.test.tsx
index f4c7299e..e65aae92 100644
--- a/tests/features/reference-feature/reference-page.test.tsx
+++ b/tests/features/reference-feature/reference-page.test.tsx
@@ -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 },
})}
>
-
+
+
+