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..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"; @@ -278,7 +278,7 @@ function describeNavigation( }; const pathname = () => { const t = to(); - return t === undefined ? undefined : new URL(t, mockBase).pathname; + return t === undefined ? undefined : locationURL(t).pathname; }; const ref: NavigationRef = { kind: "navigation", @@ -336,7 +336,7 @@ function describeInitial( location: () => LocationChange ): NavigationRef { const to = () => untrack(location).value; - const pathname = () => new URL(to(), mockBase).pathname; + const pathname = () => locationURL(to()).pathname; return { kind: "navigation", initial: true, @@ -495,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" ? new URL(url, mockBase).pathname : ((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) : ""; 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({