From 3a78506e4f96e0c8df5ce705b50b3a4db466a711 Mon Sep 17 00:00:00 2001 From: Junaid Khan Date: Wed, 25 Nov 2026 18:31:31 +0530 Subject: [PATCH] fix(router): match hashHistory links via parsePath Claimed anchors and useLinkState compared URL pathname alone, so `#/applications` looked like `/` and aria-current/data-active were wrong. Parse pathname+search+hash like the click handler (#746). --- src/claims.ts | 13 +++++- src/routing.ts | 9 +++- test/claims.spec.tsx | 103 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 5 deletions(-) diff --git a/src/claims.ts b/src/claims.ts index 34dc898c..a326a5c4 100644 --- a/src/claims.ts +++ b/src/claims.ts @@ -90,7 +90,12 @@ export function setupLinkClaims( const location = router.location; const routing = plugin && plugin.track(); const url = managedUrl(a); - const target = url && url.pathname + url.search; + // Include hash so hashHistory's parsePath can recover `/applications` + // from an href like `#/applications` (URL pathname alone is `/`). Same + // spelling the click handler feeds parsePath (#746). + const target = + url && + router.parsePath(url.pathname + url.search + (("hash" in url && url.hash) || "")); // no per-anchor `end` opt-out like useLinkState has const { active, current } = match(target); const pending = !!routing && plugin!.pending(target); @@ -257,7 +262,11 @@ export function setupServerLinkClaims( basePath ); if (!url) return ""; - const { active, current } = match(url.pathname + url.search, url.comparable); + // Same parsePath path as the client claims / click handler (#746). + const target = router.parsePath( + url.pathname + url.search + (("hash" in url && url.hash) || "") + ); + const { active, current } = match(target, url.comparable); return current ? ' data-active aria-current="page"' : active ? " data-active" : ""; }); } diff --git a/src/routing.ts b/src/routing.ts index 71f5d0f5..3bada8b6 100644 --- a/src/routing.ts +++ b/src/routing.ts @@ -406,10 +406,15 @@ export const useLinkState = ( ): LinkState => { const router = useRouter(); const location = router.location; - // a paths node's brand is its logical path; toString is the display href + // a paths node's brand is its logical path; toString is the display href. + // Display strings under hashHistory start with `#` — map them back through + // parsePath the same way navigate() does, so `useLinkState("#/about")` + // matches location.pathname `/about` (#746). const to = useResolvedPath(() => { const h: any = href(); - return h[HREF] || String(h); + let path: string = h[HREF] || String(h); + if (path[0] === "#") path = router.parsePath(path); + return path; }); const base = router.base.path(); const state = createMemo(() => linkMatcher(location, base, options.end)(to())); diff --git a/test/claims.spec.tsx b/test/claims.spec.tsx index fff1cf26..93f04fb2 100644 --- a/test/claims.spec.tsx +++ b/test/claims.spec.tsx @@ -16,7 +16,7 @@ import { } from "solid-js"; import { vi } from "vitest"; import { setupLinkClaims } from "../src/claims.js"; -import { createRouter, memoryHistory, pendingLinks } from "../src/index.js"; +import { createRouter, hashHistory, memoryHistory, pendingLinks } from "../src/index.js"; import type { LinkState, Navigator } from "../src/index.js"; import { useLinkState, useNavigate } from "../src/index.js"; import type { RouterContext } from "../src/types.js"; @@ -1163,3 +1163,104 @@ describe("link state parity: claimed anchors and useLinkState", () => { } }); }); + +describe("hashHistory link claims and useLinkState (#746)", () => { + const originalScrollTo = window.scrollTo; + beforeEach(() => { + window.scrollTo = vi.fn() as any; + window.history.replaceState(null, "", "/#/"); + }); + afterEach(() => { + window.history.replaceState(null, "", "/"); + }); + afterAll(() => { + window.scrollTo = originalScrollTo; + }); + + const routes = [ + { path: "/", component: () =>
}, + { path: "/applications", component: () =>
} + ] as const; + + test("only the current hash link gets aria-current / data-active", async () => { + const div = mount(); + let navigate!: Navigator; + const Router = createRouter({ routes, history: hashHistory() }); + const dispose = render( + () => ( + + {props => { + navigate = useNavigate(); + return ( + + ); + }} + + ), + div + ); + try { + await settle(); + const home = div.querySelector('[data-testid="home"]')!; + const applications = div.querySelector('[data-testid="applications"]')!; + + expect(home.getAttribute("aria-current")).toBe("page"); + expect(home.hasAttribute("data-active")).toBe(true); + expect(applications.hasAttribute("aria-current")).toBe(false); + expect(applications.hasAttribute("data-active")).toBe(false); + + navigate("/applications"); + await settle(); + + expect(applications.getAttribute("aria-current")).toBe("page"); + expect(applications.hasAttribute("data-active")).toBe(true); + expect(home.hasAttribute("aria-current")).toBe(false); + expect(home.hasAttribute("data-active")).toBe(false); + } finally { + dispose(); + div.remove(); + } + }); + + test("useLinkState understands display hash hrefs and paths nodes", async () => { + window.history.replaceState(null, "", "/#/applications"); + const Router = createRouter({ routes, history: hashHistory() }); + let hashHref!: LinkState; + let pathsNode!: LinkState; + const div = mount(); + const dispose = render( + () => ( + + {p => ( + <> + { + ((hashHref = useLinkState(() => "#/applications")), + (pathsNode = useLinkState(() => Router.paths.applications)), + null) + } + {p.children} + + )} + + ), + div + ); + try { + await settle(); + expect(String(Router.paths.applications)).toBe("#/applications"); + expect([hashHref.active(), hashHref.current()]).toEqual([true, true]); + expect([pathsNode.active(), pathsNode.current()]).toEqual([true, true]); + } finally { + dispose(); + div.remove(); + } + }); +});