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/observe-double-slash-path.md
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions src/routers/factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -495,7 +495,7 @@ export function createRouter<const R extends readonly RouteDefinition[]>(
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
Expand Down
7 changes: 3 additions & 4 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import type {
} from "./types.js";
import {
mockBase,
locationURL,
createMemoObject,
extractSearchParams,
invariant,
Expand Down Expand Up @@ -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;
},
{
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : "";
Expand Down
17 changes: 17 additions & 0 deletions test/observe-navigation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => <div data-route="home">Home</div> }] 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({
Expand Down