From e28b0181b9c6311a22a47b3116295c2fc9343874 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 23 Nov 2026 13:13:07 -0500 Subject: [PATCH 1/3] fix: keep NavLink inactive in the SPA fallback --- integration/vite-spa-mode-test.ts | 111 ++++++++++++++++++ .../.changes/patch.spa-navlink-hydration.md | 1 + packages/react-router/lib/dom/lib.tsx | 26 +++- 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 packages/react-router/.changes/patch.spa-navlink-hydration.md diff --git a/integration/vite-spa-mode-test.ts b/integration/vite-spa-mode-test.ts index 12f9ebbcde..3d0ec7846a 100644 --- a/integration/vite-spa-mode-test.ts +++ b/integration/vite-spa-mode-test.ts @@ -1365,4 +1365,115 @@ test.describe("SPA Mode", () => { "app/routes/about.tsx", ]); }); + + test("hydrates NavLink active state for a deep link", async ({ page }) => { + let fixture = await createFixture({ + spaMode: true, + files: { + "app/root.tsx": js` + import { lazy, Suspense, useEffect, useState } from "react"; + import { Outlet, Scripts, useSearchParams } from "react-router"; + + const Menu = lazy(() => import("./layout-menu")); + + export function Layout({ children }: { children: React.ReactNode }) { + let [searchParams] = useSearchParams(); + let [initialTab] = useState(() => searchParams.get("tab") ?? ""); + useEffect(() => { + document.body.dataset.initialTab = initialTab; + }, [initialTab]); + + return ( + + + + + + + + + + {children} + + + + ); + } + + export default function App() { + return ; + } + `, + "app/layout-menu.tsx": js` + import { NavLink } from "react-router"; + + export default function Menu() { + return ( + + ); + } + `, + "app/routes/_index.tsx": js` + export default function Index() { + return

Home page

; + } + `, + "app/routes/other.tsx": js` + export default function Other() { + return

Other page

; + } + `, + }, + }); + let appFixture = await createAppFixture(fixture); + let menu = Promise.withResolvers(); + + try { + await page.route("**/assets/layout-menu-*.js", async (route) => { + await menu.promise; + await route.continue(); + }); + let html = await (await fixture.requestDocument("/")).text(); + let anchors = html.match(/]*>/g) ?? []; + expect(anchors).toHaveLength(2); + for (let anchor of anchors) { + expect(anchor).not.toMatch(/\bactive\b|aria-current=/); + } + + let app = new PlaywrightFixture(appFixture, page); + await app.goto("/other?tab=details", false); + await expect(page.locator("body")).toHaveAttribute( + "data-initial-tab", + "details", + ); + await expect(page.locator("nav a")).toHaveCount(2); + await expect(page.locator("nav a.active")).toHaveCount(0); + await expect(page.locator("nav a[aria-current]")).toHaveCount(0); + menu.resolve(); + await expect(page.locator("h1")).toHaveText("Other page"); + await expect(page.locator('a[href="/"]')).not.toHaveClass(/\bactive\b/); + await expect(page.locator('a[href="/other"]')).toHaveClass(/\bactive\b/); + await expect(page.locator('a[href="/other"]')).toHaveAttribute( + "aria-current", + "page", + ); + + await app.clickLink("/"); + await expect(page.locator("h1")).toHaveText("Home page"); + await expect(page.locator("body")).toHaveAttribute( + "data-initial-tab", + "details", + ); + await expect(page.locator('a[href="/"]')).toHaveClass(/\bactive\b/); + await expect(page.locator('a[href="/other"]')).not.toHaveClass( + /\bactive\b/, + ); + } finally { + menu.resolve(); + appFixture.close(); + } + }); }); diff --git a/packages/react-router/.changes/patch.spa-navlink-hydration.md b/packages/react-router/.changes/patch.spa-navlink-hydration.md new file mode 100644 index 0000000000..05ba78dc5b --- /dev/null +++ b/packages/react-router/.changes/patch.spa-navlink-hydration.md @@ -0,0 +1 @@ +Keep `NavLink` inactive in the prerendered Framework SPA fallback and set its active state from the browser URL after hydration diff --git a/packages/react-router/lib/dom/lib.tsx b/packages/react-router/lib/dom/lib.tsx index f3d4f46792..27c91508ff 100644 --- a/packages/react-router/lib/dom/lib.tsx +++ b/packages/react-router/lib/dom/lib.tsx @@ -1586,6 +1586,9 @@ export interface NavLinkProps extends Omit< | ((props: NavLinkRenderProps) => React.CSSProperties | undefined); } +const subscribeToNothing = () => () => {}; +const getClientSpaFallback = () => false; + /** * Wraps {@link Link | ``} with additional props for styling active and * pending states. @@ -1648,12 +1651,23 @@ export const NavLink = React.forwardRef( let location = useLocation(); let routerNavigation = React.useContext(DataRouterNavigationContext); let { navigator, basename } = React.useContext(NavigationContext); + let isSpaMode = React.useContext(FrameworkContext)?.isSpaMode === true; + + // SPA fallback HTML is reused across URLs. Keep the link state neutral + // until this link hydrates, including links inside a Suspense boundary. + let isSpaFallback = React.useSyncExternalStore( + subscribeToNothing, + getClientSpaFallback, + () => isSpaMode, + ); + let isTransitioning = routerNavigation != null && // Conditional usage is OK here because the usage of a data router is static // eslint-disable-next-line react-hooks/rules-of-hooks useViewTransitionState(path) && - viewTransition === true; + viewTransition === true && + !isSpaFallback; let toPathname = navigator.encodeLocation ? navigator.encodeLocation(path).pathname @@ -1686,12 +1700,14 @@ export const NavLink = React.forwardRef( ? toPathname.length - 1 : toPathname.length; let isActive = - locationPathname === toPathname || - (!end && - locationPathname.startsWith(toPathname) && - locationPathname.charAt(endSlashPosition) === "/"); + !isSpaFallback && + (locationPathname === toPathname || + (!end && + locationPathname.startsWith(toPathname) && + locationPathname.charAt(endSlashPosition) === "/")); let isPending = + !isSpaFallback && nextLocationPathname != null && (nextLocationPathname === toPathname || (!end && From 11669c78ba4644d40d32765f90ea6240d7d31523 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 23 Nov 2026 13:19:30 -0500 Subject: [PATCH 2/3] test: use an ES2022-compatible promise for SPA hydration --- integration/vite-spa-mode-test.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/integration/vite-spa-mode-test.ts b/integration/vite-spa-mode-test.ts index 3d0ec7846a..cc7f34ae87 100644 --- a/integration/vite-spa-mode-test.ts +++ b/integration/vite-spa-mode-test.ts @@ -1429,11 +1429,14 @@ test.describe("SPA Mode", () => { }, }); let appFixture = await createAppFixture(fixture); - let menu = Promise.withResolvers(); + let releaseMenu!: () => void; + let menuReady = new Promise((resolve) => { + releaseMenu = resolve; + }); try { await page.route("**/assets/layout-menu-*.js", async (route) => { - await menu.promise; + await menuReady; await route.continue(); }); let html = await (await fixture.requestDocument("/")).text(); @@ -1452,7 +1455,7 @@ test.describe("SPA Mode", () => { await expect(page.locator("nav a")).toHaveCount(2); await expect(page.locator("nav a.active")).toHaveCount(0); await expect(page.locator("nav a[aria-current]")).toHaveCount(0); - menu.resolve(); + releaseMenu(); await expect(page.locator("h1")).toHaveText("Other page"); await expect(page.locator('a[href="/"]')).not.toHaveClass(/\bactive\b/); await expect(page.locator('a[href="/other"]')).toHaveClass(/\bactive\b/); @@ -1472,7 +1475,7 @@ test.describe("SPA Mode", () => { /\bactive\b/, ); } finally { - menu.resolve(); + releaseMenu(); appFixture.close(); } }); From 56af673e8b049eeb834df8cc090f919f1db94eb0 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 23 Nov 2026 13:39:14 -0500 Subject: [PATCH 3/3] test: verify SPA NavLink preserves hydrated markup --- integration/vite-spa-mode-test.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/integration/vite-spa-mode-test.ts b/integration/vite-spa-mode-test.ts index cc7f34ae87..7dd1b168bb 100644 --- a/integration/vite-spa-mode-test.ts +++ b/integration/vite-spa-mode-test.ts @@ -1447,7 +1447,10 @@ test.describe("SPA Mode", () => { } let app = new PlaywrightFixture(appFixture, page); - await app.goto("/other?tab=details", false); + await Promise.all([ + page.waitForRequest("**/assets/layout-menu-*.js"), + app.goto("/other?tab=details", false), + ]); await expect(page.locator("body")).toHaveAttribute( "data-initial-tab", "details", @@ -1455,14 +1458,20 @@ test.describe("SPA Mode", () => { await expect(page.locator("nav a")).toHaveCount(2); await expect(page.locator("nav a.active")).toHaveCount(0); await expect(page.locator("nav a[aria-current]")).toHaveCount(0); + let otherLink = page.locator('a[href="/other"]'); + let prerenderedOtherLink = await otherLink.evaluateHandle((link) => link); releaseMenu(); await expect(page.locator("h1")).toHaveText("Other page"); await expect(page.locator('a[href="/"]')).not.toHaveClass(/\bactive\b/); - await expect(page.locator('a[href="/other"]')).toHaveClass(/\bactive\b/); - await expect(page.locator('a[href="/other"]')).toHaveAttribute( - "aria-current", - "page", - ); + await expect(otherLink).toHaveClass(/\bactive\b/); + await expect(otherLink).toHaveAttribute("aria-current", "page"); + expect( + await otherLink.evaluate( + (link, original) => link === original, + prerenderedOtherLink, + ), + ).toBe(true); + await expect(page.locator("nav a[aria-current]")).toHaveCount(1); await app.clickLink("/"); await expect(page.locator("h1")).toHaveText("Home page"); @@ -1471,9 +1480,7 @@ test.describe("SPA Mode", () => { "details", ); await expect(page.locator('a[href="/"]')).toHaveClass(/\bactive\b/); - await expect(page.locator('a[href="/other"]')).not.toHaveClass( - /\bactive\b/, - ); + await expect(otherLink).not.toHaveClass(/\bactive\b/); } finally { releaseMenu(); appFixture.close();