Skip to content

Commit 03a362a

Browse files
committed
Remove fallback url origin in favor of invariant
1 parent 072311f commit 03a362a

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

packages/router/history.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ export function createHashHistory(
447447
//#region UTILS
448448
////////////////////////////////////////////////////////////////////////////////
449449

450+
/**
451+
* @private
452+
*/
453+
export function invariant(value: boolean, message?: string): asserts value;
454+
export function invariant<T>(
455+
value: T | null | undefined,
456+
message?: string
457+
): asserts value is T;
458+
export function invariant(value: any, message?: string) {
459+
if (value === false || value === null || typeof value === "undefined") {
460+
throw new Error(message);
461+
}
462+
}
463+
450464
function warning(cond: any, message: string) {
451465
if (!cond) {
452466
// eslint-disable-next-line no-console
@@ -553,9 +567,13 @@ export function createClientSideURL(location: Location | string): URL {
553567
typeof window.location !== "undefined" &&
554568
window.location.origin !== "null"
555569
? window.location.origin
556-
: "unknown://unknown";
570+
: null;
557571
let href = typeof location === "string" ? location : createPath(location);
558-
return new URL(href, base);
572+
invariant(
573+
base,
574+
`No window.location.origin available to create URL for href: ${href}`
575+
);
576+
return new URL(href, window.location.origin);
559577
}
560578

561579
export interface UrlHistory extends History {}

packages/router/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export {
3232
defer,
3333
generatePath,
3434
getToPathname,
35-
invariant,
3635
isRouteErrorResponse,
3736
joinPaths,
3837
json,
@@ -59,13 +58,13 @@ export type {
5958
Path,
6059
To,
6160
} from "./history";
62-
6361
export {
6462
Action,
6563
createBrowserHistory,
6664
createPath,
6765
createHashHistory,
6866
createMemoryHistory,
67+
invariant,
6968
parsePath,
7069
} from "./history";
7170

packages/router/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createLocation,
55
createPath,
66
createClientSideURL,
7+
invariant,
78
parsePath,
89
} from "./history";
910
import type {
@@ -28,7 +29,6 @@ import {
2829
ResultType,
2930
convertRoutesToDataRoutes,
3031
getPathContributingMatches,
31-
invariant,
3232
isRouteErrorResponse,
3333
joinPaths,
3434
matchRoutes,

packages/router/utils.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Location, Path, To } from "./history";
2-
import { parsePath } from "./history";
2+
import { invariant, parsePath } from "./history";
33

44
/**
55
* Map of routeId -> data returned from a loader/action/error
@@ -771,20 +771,6 @@ export function stripBasename(
771771
return pathname.slice(startIndex) || "/";
772772
}
773773

774-
/**
775-
* @private
776-
*/
777-
export function invariant(value: boolean, message?: string): asserts value;
778-
export function invariant<T>(
779-
value: T | null | undefined,
780-
message?: string
781-
): asserts value is T;
782-
export function invariant(value: any, message?: string) {
783-
if (value === false || value === null || typeof value === "undefined") {
784-
throw new Error(message);
785-
}
786-
}
787-
788774
/**
789775
* @private
790776
*/

0 commit comments

Comments
 (0)