Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed May 25, 2023
1 parent e516138 commit 0f16b2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
18 changes: 10 additions & 8 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ type SetupOpts = {
future?: FutureConfig;
};

let globalWindow = window;
function setup({
routes,
basename,
Expand Down Expand Up @@ -429,15 +428,18 @@ function setup({
});
}

// jsdom is making more and more properties non-configurable, so we inject our own jest-friendly window 😅
let window = {
...globalWindow,
// jsdom is making more and more properties non-configurable, so we inject
// our own jest-friendly window.
let testWindow = {
...window,
location: {
...globalWindow.location,
...window.location,
assign: jest.fn(),
replace: jest.fn(),
},
} as unknown as Window; // spread makes TS sad, since `window.NaN` conflicts with the `[index: number]: Window` index signature
} as unknown as Window;
// ^ Spread makes TS sad - `window.NaN` conflicts with `[index: number]: Window`

let history = createMemoryHistory({ initialEntries, initialIndex });
jest.spyOn(history, "push");
jest.spyOn(history, "replace");
Expand All @@ -447,7 +449,7 @@ function setup({
routes: enhanceRoutes(routes),
hydrationData,
future,
window,
window: testWindow,
}).initialize();

function getRouteHelpers(
Expand Down Expand Up @@ -854,7 +856,7 @@ function setup({
}

return {
window,
window: testWindow,
history,
router: currentRouter,
navigate,
Expand Down
30 changes: 13 additions & 17 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,6 @@ export const IDLE_BLOCKER: BlockerUnblocked = {

const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;

const globalWindow = typeof window !== "undefined" ? window : undefined;

const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
hasErrorBoundary: Boolean(route.hasErrorBoundary),
});
Expand All @@ -677,14 +675,16 @@ const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
/**
* Create a router and listen to history POP navigations
*/
export function createRouter({
window = globalWindow,
...init
}: RouterInit): Router {
export function createRouter(init: RouterInit): Router {
const routerWindow = init.window
? init.window
: typeof window !== "undefined"
? window
: undefined;
const isBrowser =
typeof window !== "undefined" &&
typeof window.document !== "undefined" &&
typeof window.document.createElement !== "undefined";
typeof routerWindow !== "undefined" &&
typeof routerWindow.document !== "undefined" &&
typeof routerWindow.document.createElement !== "undefined";
const isServer = !isBrowser;

invariant(
Expand Down Expand Up @@ -2088,19 +2088,15 @@ export function createRouter({
"Expected a location on the redirect navigation"
);
// Check if this an absolute external redirect that goes to a new origin
if (
ABSOLUTE_URL_REGEX.test(redirect.location) &&
isBrowser &&
typeof window?.location !== "undefined"
) {
if (ABSOLUTE_URL_REGEX.test(redirect.location) && isBrowser) {
let url = init.history.createURL(redirect.location);
let isDifferentBasename = stripBasename(url.pathname, basename) == null;

if (window.location.origin !== url.origin || isDifferentBasename) {
if (routerWindow.location.origin !== url.origin || isDifferentBasename) {
if (replace) {
window.location.replace(redirect.location);
routerWindow.location.replace(redirect.location);
} else {
window.location.assign(redirect.location);
routerWindow.location.assign(redirect.location);
}
return;
}
Expand Down

0 comments on commit 0f16b2d

Please sign in to comment.