Skip to content

Commit

Permalink
Remove fallback url origin in favor of invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 5, 2022
1 parent 072311f commit 03a362a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
22 changes: 20 additions & 2 deletions packages/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ export function createHashHistory(
//#region UTILS
////////////////////////////////////////////////////////////////////////////////

/**
* @private
*/
export function invariant(value: boolean, message?: string): asserts value;
export function invariant<T>(
value: T | null | undefined,
message?: string
): asserts value is T;
export function invariant(value: any, message?: string) {
if (value === false || value === null || typeof value === "undefined") {
throw new Error(message);
}
}

function warning(cond: any, message: string) {
if (!cond) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -553,9 +567,13 @@ export function createClientSideURL(location: Location | string): URL {
typeof window.location !== "undefined" &&
window.location.origin !== "null"
? window.location.origin
: "unknown://unknown";
: null;
let href = typeof location === "string" ? location : createPath(location);
return new URL(href, base);
invariant(
base,
`No window.location.origin available to create URL for href: ${href}`
);
return new URL(href, window.location.origin);
}

export interface UrlHistory extends History {}
Expand Down
3 changes: 1 addition & 2 deletions packages/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export {
defer,
generatePath,
getToPathname,
invariant,
isRouteErrorResponse,
joinPaths,
json,
Expand All @@ -59,13 +58,13 @@ export type {
Path,
To,
} from "./history";

export {
Action,
createBrowserHistory,
createPath,
createHashHistory,
createMemoryHistory,
invariant,
parsePath,
} from "./history";

Expand Down
2 changes: 1 addition & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createLocation,
createPath,
createClientSideURL,
invariant,
parsePath,
} from "./history";
import type {
Expand All @@ -28,7 +29,6 @@ import {
ResultType,
convertRoutesToDataRoutes,
getPathContributingMatches,
invariant,
isRouteErrorResponse,
joinPaths,
matchRoutes,
Expand Down
16 changes: 1 addition & 15 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Location, Path, To } from "./history";
import { parsePath } from "./history";
import { invariant, parsePath } from "./history";

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

/**
* @private
*/
export function invariant(value: boolean, message?: string): asserts value;
export function invariant<T>(
value: T | null | undefined,
message?: string
): asserts value is T;
export function invariant(value: any, message?: string) {
if (value === false || value === null || typeof value === "undefined") {
throw new Error(message);
}
}

/**
* @private
*/
Expand Down

0 comments on commit 03a362a

Please sign in to comment.