Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/client-drop-plain-href.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Client builds no longer ship `plainHref` and its regex, which only server link claims call.
5 changes: 5 additions & 0 deletions .changeset/plain-href-link-claim-miss.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 20 additions & 2 deletions src/claims.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`.
Expand Down
65 changes: 64 additions & 1 deletion test/server/plain-href-fast-path.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
});
});