diff --git a/.changeset/client-drop-plain-href.md b/.changeset/client-drop-plain-href.md new file mode 100644 index 00000000..df84c99f --- /dev/null +++ b/.changeset/client-drop-plain-href.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Client builds no longer ship `plainHref` and its regex, which only server link claims call. diff --git a/.changeset/plain-href-link-claim-miss.md b/.changeset/plain-href-link-claim-miss.md new file mode 100644 index 00000000..5173e928 --- /dev/null +++ b/.changeset/plain-href-link-claim-miss.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Server link claims return early for a plain root-relative href that cannot be the current location or one of its parents, so those links skip href resolution and matching. diff --git a/src/claims.ts b/src/claims.ts index 34dc898c..89d25ab9 100644 --- a/src/claims.ts +++ b/src/claims.ts @@ -1,7 +1,13 @@ import { hasServerLinkState, isServer, registerElementClaim, setLinkClaim } from "@solidjs/web"; import { createRenderEffect, getOwner, isHydrating, onCleanup, untrack } from "solid-js"; import type { LinksPlugin, RouterContext } from "./types.js"; -import { isUnderBase, linkMatcher, plainHref } from "./utils.js"; +import { + comparablePath, + isUnderBase, + linkMatcher, + serverPlainHref, + serverPlainPath +} from "./utils.js"; /** * Claimed forms are handed to this slot instead of the claims importing the @@ -199,7 +205,7 @@ function managedLinkUrl( if (a.rel && a.rel.split(/\s+/).includes("external")) return; // server pages resolve root-relative hrefs against `origin`, so plain ones skip `URL` let url: { pathname: string; search: string; comparable?: string } | undefined = isServer - ? plainHref(a.href) + ? serverPlainHref!(a.href) : undefined; if (!url) { let parsed; @@ -233,6 +239,13 @@ function locationMatcher(router: RouterContext, basePath: string) { // a raw SSR attribute value as the DOM would read it back const attr = (v: unknown) => (v == null || v === false ? null : v === true ? "" : String(v)); +/** Whether a plain href's path can't be `loc` or a parent of it; lives here so client builds drop it. */ +export function plainHrefMisses(href: string, loc: string) { + if (!serverPlainPath!(href) || href[0] !== "/" || href[1] === "/" || href.endsWith("/")) + return false; + return href.length > loc.length || !loc.startsWith(href.toLowerCase()); +} + /** Server link handler: marks anchors by the client's rule, resolved against `page`. */ export function setupServerLinkClaims( router: RouterContext, @@ -242,7 +255,12 @@ export function setupServerLinkClaims( const basePath = router.base.path(); const origin = page.origin; const match = locationMatcher(router, basePath); + let pathname: string | undefined; + let loc = ""; setLinkClaim(attrs => { + const locationPath = router.location.pathname; + if (locationPath !== pathname) loc = comparablePath((pathname = locationPath)); + if (typeof attrs.href === "string" && plainHrefMisses(attrs.href, loc)) return ""; const url = managedLinkUrl( { href: attr(attrs.href), diff --git a/src/utils.ts b/src/utils.ts index 1ec899bc..e2992d30 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -42,6 +42,9 @@ const hasDotSegment = (path: string) => path.includes("/.") && dotSegmentRegex.t /** Whether `URL` would report this pathname unchanged, so it can skip the parse. */ const isPlainPath = (path: string) => plainPathRegex.test(path) && !hasDotSegment(path); +/** `isPlainPath` for server-only callers in other modules; `undefined` in client builds so they drop it. */ +export const serverPlainPath = isServer ? isPlainPath : undefined; + /** A root-relative href's pathname, search and comparable path, or `undefined` if `URL` would change them. */ export function plainHref(href: string) { const m = plainHrefRegex.exec(href); @@ -53,6 +56,9 @@ export function plainHref(href: string) { return { pathname, search: m[2] ? "?" + m[2] : "", comparable }; } +/** `plainHref` for server-only callers in other modules; `undefined` in client builds so they drop it. */ +export const serverPlainHref = isServer ? plainHref : undefined; + /** * Whether a URL path (query and hash ignored) is the router's base path or under it, on a segment * boundary: base `/app` covers `/app`, `/app/` and `/app/x`, not `/apple`. diff --git a/test/server/plain-href-fast-path.spec.ts b/test/server/plain-href-fast-path.spec.ts index 95be3c0b..e499b70f 100644 --- a/test/server/plain-href-fast-path.spec.ts +++ b/test/server/plain-href-fast-path.spec.ts @@ -1,5 +1,12 @@ import { describe, expect, test } from "vitest"; -import { comparablePath, mockBase, normalizePath, plainHref } from "../../src/utils.js"; +import { + comparablePath, + linkMatcher, + mockBase, + normalizePath, + plainHref +} from "../../src/utils.js"; +import { plainHrefMisses } from "../../src/claims.js"; const viaUrl = (path: string) => new URL(mockBase + normalizePath(path.split(/[?#]/, 1)[0])).pathname @@ -51,3 +58,59 @@ describe("plain path fast path", () => { }); }); }); + +describe("plainHrefMisses", () => { + const state = (href: string, location: string) => { + const url = new URL(href, "http://sr/page/here?x=1"); + return linkMatcher({ pathname: location, search: "" }, "")(url.pathname + url.search); + }; + + test("never rejects a link the matcher would mark", () => { + const r = rand(2); + const segment = () => ["a", "B", "ab", "a.b", "%41", ".", "..", ""][Math.floor(r() * 8)]; + const path = () => { + let s = ""; + for (let n = Math.floor(r() * 4); n >= 0; n--) s += "/" + segment(); + return s; + }; + const suffix = ["", "", "", "/", "//", "?x=1", "/?x=1", "#y", "/#y"]; + let misses = 0; + let active = 0; + for (let i = 0; i < 100000; i++) { + const location = new URL("http://sr" + path()).pathname; + const href = (r() < 0.5 ? location : path()) + suffix[Math.floor(r() * suffix.length)]; + if (!URL.canParse(href, "http://sr/page/here?x=1")) continue; + const { active: a, current } = state(href, location); + if (a) active++; + if (plainHrefMisses(href, comparablePath(location))) { + misses++; + expect(a || current, `${href} on ${location}`).toBe(false); + } + } + expect(misses).toBeGreaterThan(1000); + expect(active).toBeGreaterThan(1000); + }); + + test("rejects unrelated plain paths and leaves the rest to the matcher", () => { + for (const [href, loc] of [ + ["/server-side-rendered/1", "/server-side-rendered"], + ["/a", ""], + ["/About/x", "/about"] + ]) + expect(plainHrefMisses(href, loc), href).toBe(true); + for (const [href, loc] of [ + ["/a", "/a"], + ["/A/", "/a"], + ["/a", "/a/b"], + ["/a?x=1", "/a"], + ["/b?x=1", "/a"], + ["/a//#y", "/a"], + ["/", ""], + ["/a/../b", "/b"], + ["/%61", "/a"], + ["relative", "/a"], + ["//x.example/a", "/a"] + ]) + expect(plainHrefMisses(href, loc), href).toBe(false); + }); +});