Skip to content

Commit 457ac72

Browse files
perf(claims): parse location and base once per link sweep (#665)
Co-authored-by: Cursor <[email protected]>
1 parent a9e2f6a commit 457ac72

5 files changed

Lines changed: 43 additions & 25 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
Claimed anchors share one parse of the location and base path instead of
6+
re-parsing both per anchor, roughly halving the `URL` constructions link
7+
state does at load and on every navigation sweep.

‎src/claims.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { registerElementClaim } from "@solidjs/web";
22
import { createRenderEffect, getOwner, onCleanup, untrack } from "solid-js";
33
import type { LinksPlugin, RouterContext } from "./types.js";
4-
import { isUnderBase, matchLink } from "./utils.js";
4+
import { isUnderBase, linkMatcher } from "./utils.js";
55

66
/**
77
* Claimed forms are handed to this slot instead of the claims importing the
@@ -30,7 +30,7 @@ export function setFormClaimHandler(handler: ((form: HTMLFormElement) => void) |
3030
* opt-in through `createRouter({ links: pendingLinks })`. Without the
3131
* plugin, claims never read pending state and the sweep does not track it.
3232
*
33-
* The matching rule is `matchLink`, shared with `useLinkState`. The router
33+
* The matching rule is `linkMatcher`, shared with `useLinkState`. The router
3434
* only touches an `aria-current` it wrote itself: one the author set (a
3535
* stepper's `"step"`, a static `"page"`) is left in place, current or not.
3636
*
@@ -81,15 +81,25 @@ export function setupLinkClaims(
8181
return url;
8282
}
8383

84+
// every anchor matches against the same location, so its parse is shared
85+
// until the location changes
86+
let matched: string | undefined;
87+
let match: ReturnType<typeof linkMatcher>;
88+
8489
function linkState(a: HTMLAnchorElement | SVGAElement) {
8590
// read reactive sources unconditionally so the owning effect stays
8691
// subscribed even while the anchor is not router-managed
8792
const location = router.location;
8893
const routing = plugin && plugin.track();
8994
const url = managedUrl(a);
9095
const target = url && url.pathname + url.search;
96+
const key = location.pathname + location.search;
9197
// no per-anchor `end` opt-out like useLinkState has
92-
const { active, current } = matchLink(location, target, basePath);
98+
if (key !== matched) {
99+
matched = key;
100+
match = linkMatcher(location, basePath);
101+
}
102+
const { active, current } = match(target);
93103
const pending = !!routing && plugin!.pending(target);
94104
return { active, pending, current };
95105
}

‎src/pending.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createMemo, DEV, isPending, latest, NotReadyError, runWithOwner } from "solid-js";
22
import type { LinksPlugin, LocationChange, RouterContext } from "./types.js";
3-
import { matchLink } from "./utils.js";
3+
import { linkMatcher } from "./utils.js";
44

55
/**
66
* Pending navigation state, for the opt-in readers only: `useIsRouting`,
@@ -59,7 +59,7 @@ export function linkPending(
5959
end?: boolean
6060
): boolean {
6161
const target = pendingTarget(router);
62-
return !!target && matchLink({ pathname: target.value, search: "" }, to, base, end).active;
62+
return !!target && linkMatcher({ pathname: target.value, search: "" }, base, end)(to).active;
6363
}
6464

6565
/**

‎src/routing.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
createMemoObject,
4949
extractSearchParams,
5050
invariant,
51-
matchLink,
51+
linkMatcher,
5252
normalizePath,
5353
resolvePath,
5454
createMatcher,
@@ -409,7 +409,7 @@ export const useLinkState = (
409409
const location = router.location;
410410
const to = useResolvedPath(() => String(href()));
411411
const base = router.base.path();
412-
const state = createMemo(() => matchLink(location, to(), base, options.end));
412+
const state = createMemo(() => linkMatcher(location, base, options.end)(to()));
413413
return {
414414
active: createMemo(() => state().active),
415415
current: createMemo(() => state().current),

‎src/utils.ts‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,33 @@ const comparableQuery = (search: string) => {
4848
};
4949

5050
/**
51-
* The link-state rule shared by claimed anchors and `useLinkState`, given
52-
* the location and a link's already-resolved target (path, optional query
53-
* and hash):
51+
* The link-state rule shared by claimed anchors and `useLinkState`, bound to
52+
* a location so links matched against the same one share its parse. The
53+
* returned matcher takes a link's already-resolved target (path, optional
54+
* query and hash):
5455
*
5556
* - `current` — same pathname and same query (parameter order and hash aside)
5657
* - `active` — same pathname or one under it; the router's root (`base`, or
5758
* `/` without one) only matches exactly, since it is a prefix of every
5859
* page. `end` makes every link exact-only.
5960
*/
60-
export function matchLink(
61-
location: { pathname: string; search: string },
62-
target: string | undefined,
61+
export function linkMatcher(
62+
{ pathname, search }: { pathname: string; search: string },
6363
base: string,
6464
end?: boolean
65-
): { active: boolean; current: boolean } {
66-
if (target === undefined) return { active: false, current: false };
67-
const loc = comparablePath(location.pathname);
68-
const path = comparablePath(target);
69-
const exact = loc === path;
70-
const hashless = target.split("#", 1)[0];
71-
const q = hashless.indexOf("?");
72-
return {
73-
active:
74-
exact || (!end && path !== "" && path !== comparablePath(base) && loc.startsWith(path + "/")),
75-
current:
76-
exact && comparableQuery(location.search) === comparableQuery(q < 0 ? "" : hashless.slice(q))
65+
) {
66+
const loc = comparablePath(pathname);
67+
const root = comparablePath(base);
68+
return (target: string | undefined): { active: boolean; current: boolean } => {
69+
if (target === undefined) return { active: false, current: false };
70+
const path = comparablePath(target);
71+
const exact = loc === path;
72+
const hashless = target.split("#", 1)[0];
73+
const q = hashless.indexOf("?");
74+
return {
75+
active: exact || (!end && path !== "" && path !== root && loc.startsWith(path + "/")),
76+
current: exact && comparableQuery(search) === comparableQuery(q < 0 ? "" : hashless.slice(q))
77+
};
7778
};
7879
}
7980

0 commit comments

Comments
 (0)