Skip to content

Commit bd58142

Browse files
authored
Fix absolute redirect detection (#9689)
* Fix absolute redirect detection * Use createClientSideUrl * inline origin * Add changeset
1 parent 51e65db commit bd58142

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

.changeset/swift-lemons-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Fix URL creation in Remix integration tests

packages/router/router.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,17 +1606,17 @@ export function createRouter(init: RouterInit): Router {
16061606
"Expected a location on the redirect navigation"
16071607
);
16081608

1609-
if (
1610-
redirect.external &&
1611-
typeof window !== "undefined" &&
1612-
typeof window.location !== "undefined"
1613-
) {
1614-
if (replace) {
1615-
window.location.replace(redirect.location);
1616-
} else {
1617-
window.location.assign(redirect.location);
1609+
// Check if this an external redirect that goes to a new origin
1610+
if (typeof window?.location !== "undefined") {
1611+
let newOrigin = createClientSideURL(redirect.location).origin;
1612+
if (window.location.origin !== newOrigin) {
1613+
if (replace) {
1614+
window.location.replace(redirect.location);
1615+
} else {
1616+
window.location.assign(redirect.location);
1617+
}
1618+
return;
16181619
}
1619-
return;
16201620
}
16211621

16221622
// There's no need to abort on redirects, since we don't detect the
@@ -2627,22 +2627,19 @@ async function callLoaderOrAction(
26272627
"Redirects returned/thrown from loaders/actions must have a Location header"
26282628
);
26292629

2630-
// Check if this an external redirect that goes to a new origin
2631-
let currentUrl = new URL(request.url);
2632-
let currentOrigin = currentUrl.origin;
2633-
let newOrigin = new URL(location, currentOrigin).origin;
2634-
let external = newOrigin !== currentOrigin;
2630+
let isAbsolute =
2631+
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
26352632

26362633
// Support relative routing in internal redirects
2637-
if (!external) {
2634+
if (!isAbsolute) {
26382635
let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
26392636
let routePathnames = getPathContributingMatches(activeMatches).map(
26402637
(match) => match.pathnameBase
26412638
);
26422639
let resolvedLocation = resolveTo(
26432640
location,
26442641
routePathnames,
2645-
currentUrl.pathname
2642+
new URL(request.url).pathname
26462643
);
26472644
invariant(
26482645
createPath(resolvedLocation),
@@ -2673,7 +2670,6 @@ async function callLoaderOrAction(
26732670
status,
26742671
location,
26752672
revalidate: result.headers.get("X-Remix-Revalidate") !== null,
2676-
external,
26772673
};
26782674
}
26792675

packages/router/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export interface RedirectResult {
4141
status: number;
4242
location: string;
4343
revalidate: boolean;
44-
external: boolean;
4544
}
4645

4746
/**

0 commit comments

Comments
 (0)