Skip to content

Commit 0f16b2d

Browse files
committed
Minor updates
1 parent e516138 commit 0f16b2d

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ type SetupOpts = {
302302
future?: FutureConfig;
303303
};
304304

305-
let globalWindow = window;
306305
function setup({
307306
routes,
308307
basename,
@@ -429,15 +428,18 @@ function setup({
429428
});
430429
}
431430

432-
// jsdom is making more and more properties non-configurable, so we inject our own jest-friendly window 😅
433-
let window = {
434-
...globalWindow,
431+
// jsdom is making more and more properties non-configurable, so we inject
432+
// our own jest-friendly window.
433+
let testWindow = {
434+
...window,
435435
location: {
436-
...globalWindow.location,
436+
...window.location,
437437
assign: jest.fn(),
438438
replace: jest.fn(),
439439
},
440-
} as unknown as Window; // spread makes TS sad, since `window.NaN` conflicts with the `[index: number]: Window` index signature
440+
} as unknown as Window;
441+
// ^ Spread makes TS sad - `window.NaN` conflicts with `[index: number]: Window`
442+
441443
let history = createMemoryHistory({ initialEntries, initialIndex });
442444
jest.spyOn(history, "push");
443445
jest.spyOn(history, "replace");
@@ -447,7 +449,7 @@ function setup({
447449
routes: enhanceRoutes(routes),
448450
hydrationData,
449451
future,
450-
window,
452+
window: testWindow,
451453
}).initialize();
452454

453455
function getRouteHelpers(
@@ -854,7 +856,7 @@ function setup({
854856
}
855857

856858
return {
857-
window,
859+
window: testWindow,
858860
history,
859861
router: currentRouter,
860862
navigate,

packages/router/router.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,6 @@ export const IDLE_BLOCKER: BlockerUnblocked = {
662662

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

665-
const globalWindow = typeof window !== "undefined" ? window : undefined;
666-
667665
const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
668666
hasErrorBoundary: Boolean(route.hasErrorBoundary),
669667
});
@@ -677,14 +675,16 @@ const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
677675
/**
678676
* Create a router and listen to history POP navigations
679677
*/
680-
export function createRouter({
681-
window = globalWindow,
682-
...init
683-
}: RouterInit): Router {
678+
export function createRouter(init: RouterInit): Router {
679+
const routerWindow = init.window
680+
? init.window
681+
: typeof window !== "undefined"
682+
? window
683+
: undefined;
684684
const isBrowser =
685-
typeof window !== "undefined" &&
686-
typeof window.document !== "undefined" &&
687-
typeof window.document.createElement !== "undefined";
685+
typeof routerWindow !== "undefined" &&
686+
typeof routerWindow.document !== "undefined" &&
687+
typeof routerWindow.document.createElement !== "undefined";
688688
const isServer = !isBrowser;
689689

690690
invariant(
@@ -2088,19 +2088,15 @@ export function createRouter({
20882088
"Expected a location on the redirect navigation"
20892089
);
20902090
// Check if this an absolute external redirect that goes to a new origin
2091-
if (
2092-
ABSOLUTE_URL_REGEX.test(redirect.location) &&
2093-
isBrowser &&
2094-
typeof window?.location !== "undefined"
2095-
) {
2091+
if (ABSOLUTE_URL_REGEX.test(redirect.location) && isBrowser) {
20962092
let url = init.history.createURL(redirect.location);
20972093
let isDifferentBasename = stripBasename(url.pathname, basename) == null;
20982094

2099-
if (window.location.origin !== url.origin || isDifferentBasename) {
2095+
if (routerWindow.location.origin !== url.origin || isDifferentBasename) {
21002096
if (replace) {
2101-
window.location.replace(redirect.location);
2097+
routerWindow.location.replace(redirect.location);
21022098
} else {
2103-
window.location.assign(redirect.location);
2099+
routerWindow.location.assign(redirect.location);
21042100
}
21052101
return;
21062102
}

0 commit comments

Comments
 (0)