From 0dbe95051fc99d5049d38f9fe61c1fa41b95e747 Mon Sep 17 00:00:00 2001 From: bryanlundberg <119996547+bryanlundberg@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:22:04 -0600 Subject: [PATCH 1/2] fix(observe): resolve a path that starts with // as a path (#752) --- .changeset/observe-double-slash-path.md | 5 +++++ src/routers/factory.tsx | 15 ++++++++++++--- test/observe-navigation.spec.tsx | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .changeset/observe-double-slash-path.md diff --git a/.changeset/observe-double-slash-path.md b/.changeset/observe-double-slash-path.md new file mode 100644 index 00000000..ccd4f8eb --- /dev/null +++ b/.changeset/observe-double-slash-path.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Resolve a path that starts with `//` as a path in the observe tier's navigation declarations, so a request for `//` no longer crashes the dev server with `diagnostics: true` (#752) diff --git a/src/routers/factory.tsx b/src/routers/factory.tsx index 19477ee7..33dfed32 100644 --- a/src/routers/factory.tsx +++ b/src/routers/factory.tsx @@ -249,6 +249,15 @@ export interface RouterInstance { const t = to(); - return t === undefined ? undefined : new URL(t, mockBase).pathname; + return t === undefined ? undefined : pathnameOfPath(t); }; const ref: NavigationRef = { kind: "navigation", @@ -336,7 +345,7 @@ function describeInitial( location: () => LocationChange ): NavigationRef { const to = () => untrack(location).value; - const pathname = () => new URL(to(), mockBase).pathname; + const pathname = () => pathnameOfPath(to()); return { kind: "navigation", initial: true, @@ -495,7 +504,7 @@ export function createRouter( getRouteMatches(branches(), config.transformUrl ? config.transformUrl(pathname) : pathname); // a `paths` node carries its logical pathname under the Href brand const pathnameOf = (url: string | TypedPath) => - typeof url === "string" ? new URL(url, mockBase).pathname : ((url as any)[HREF] as string); + typeof url === "string" ? pathnameOfPath(url) : ((url as any)[HREF] as string); function RouterComponent(props: RouterProps): JSX.Element { // One router per app: the session (location, history, delegation, link diff --git a/test/observe-navigation.spec.tsx b/test/observe-navigation.spec.tsx index 07b87fce..bf0fa156 100644 --- a/test/observe-navigation.spec.tsx +++ b/test/observe-navigation.spec.tsx @@ -122,6 +122,23 @@ describe("observe tier: navigations declared to attribution", () => { } }); + test("an arrival on a path that starts with // is declared without throwing (#752)", () => { + const Router = createRouter({ + routes: [{ path: "/", component: () =>
Home
}] as const, + history: memoryHistory("//") + }); + const { cleanup } = mount(Router); + try { + const nav = last(); + expect(nav.initial).toBe(true); + expect(nav.to).toBe("//"); + expect(() => nav.name).not.toThrow(); + expect(() => nav.params).not.toThrow(); + } finally { + cleanup(); + } + }); + test("the initial declaration is not a row in feedback().navigations", async () => { let navigate!: Navigator; const Router = createRouter({ From b40916d294aa70ac3078a62eabe8c9683f0d3846 Mon Sep 17 00:00:00 2001 From: bryanlundberg <119996547+bryanlundberg@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:33:26 -0600 Subject: [PATCH 2/2] refactor: share the rooted-path URL resolution in utils --- src/routers/factory.tsx | 17 ++++------------- src/routing.ts | 7 +++---- src/utils.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/routers/factory.tsx b/src/routers/factory.tsx index 33dfed32..d58df919 100644 --- a/src/routers/factory.tsx +++ b/src/routers/factory.tsx @@ -56,7 +56,7 @@ import type { TypedPath, ValidFilters } from "../types.js"; -import { mockBase } from "../utils.js"; +import { locationURL, mockBase } from "../utils.js"; import { Root, Routes } from "./components.jsx"; import { browserHistory } from "./history.js"; import type { RouterHistory } from "./history.js"; @@ -249,15 +249,6 @@ export interface RouterInstance { const t = to(); - return t === undefined ? undefined : pathnameOfPath(t); + return t === undefined ? undefined : locationURL(t).pathname; }; const ref: NavigationRef = { kind: "navigation", @@ -345,7 +336,7 @@ function describeInitial( location: () => LocationChange ): NavigationRef { const to = () => untrack(location).value; - const pathname = () => pathnameOfPath(to()); + const pathname = () => locationURL(to()).pathname; return { kind: "navigation", initial: true, @@ -504,7 +495,7 @@ export function createRouter( getRouteMatches(branches(), config.transformUrl ? config.transformUrl(pathname) : pathname); // a `paths` node carries its logical pathname under the Href brand const pathnameOf = (url: string | TypedPath) => - typeof url === "string" ? pathnameOfPath(url) : ((url as any)[HREF] as string); + typeof url === "string" ? locationURL(url).pathname : ((url as any)[HREF] as string); function RouterComponent(props: RouterProps): JSX.Element { // One router per app: the session (location, history, delegation, link diff --git a/src/routing.ts b/src/routing.ts index 71f5d0f5..cecff1ce 100644 --- a/src/routing.ts +++ b/src/routing.ts @@ -45,6 +45,7 @@ import type { } from "./types.js"; import { mockBase, + locationURL, createMemoObject, extractSearchParams, invariant, @@ -328,7 +329,7 @@ export function useSearchParams( // navigation lands on that navigation's target. navigate( headed => { - const url = new URL(headed.value[0] === "/" ? mockBase + headed.value : headed.value, mockBase); + const url = locationURL(headed.value); return url.pathname + mergeSearchString(url.search, params) + url.hash; }, { @@ -690,9 +691,7 @@ function createLocation( (prev = new URL(mockBase)) => { const path_ = path(); try { - // anchor rooted paths against the origin explicitly - a path with - // doubled leading slashes would otherwise parse as protocol-relative - return new URL(path_[0] === "/" ? mockBase + path_ : path_, mockBase); + return locationURL(path_); } catch (err) { DEV && console.error(`Invalid path ${path_}`); return prev; diff --git a/src/utils.ts b/src/utils.ts index 1ec899bc..a19e3cc3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,6 +14,14 @@ const hasSchemeRegex = /^(?:[a-z0-9]+:)?\/\//i; const trimPathRegex = /^\/+|(\/)\/+$/g; export const mockBase = "http://sr"; +/** + * A location path as a URL on `mockBase`. A rooted path is appended to the + * origin, so one that starts with `//` stays a path instead of parsing as a + * protocol-relative URL (which `new URL` rejects when the host is empty). + */ +export const locationURL = (path: string) => + new URL(path[0] === "/" ? mockBase + path : path, mockBase); + export function normalizePath(path: string, omitSlash: boolean = false) { const s = path.replace(trimPathRegex, "$1"); return s ? (omitSlash || /^[?#]/.test(s) ? s : "/" + s) : "";