From 1a7f110144bc28e9d77fd933ecc503d177016184 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 6 Dec 2022 09:26:01 -0500 Subject: [PATCH 1/4] Fix absolute redirect detection --- packages/router/router.ts | 33 +++++++++++++++------------------ packages/router/utils.ts | 1 - 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/router/router.ts b/packages/router/router.ts index 8bd5ae9f55..08b866330d 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -1606,17 +1606,18 @@ export function createRouter(init: RouterInit): Router { "Expected a location on the redirect navigation" ); - if ( - redirect.external && - typeof window !== "undefined" && - typeof window.location !== "undefined" - ) { - if (replace) { - window.location.replace(redirect.location); - } else { - window.location.assign(redirect.location); + // Check if this an external redirect that goes to a new origin + if (typeof window?.location !== "undefined") { + let currentOrigin = window.location.origin; + let newOrigin = new URL(redirect.location, currentOrigin).origin; + if (currentOrigin !== newOrigin) { + if (replace) { + window.location.replace(redirect.location); + } else { + window.location.assign(redirect.location); + } + return; } - return; } // There's no need to abort on redirects, since we don't detect the @@ -2627,14 +2628,11 @@ async function callLoaderOrAction( "Redirects returned/thrown from loaders/actions must have a Location header" ); - // Check if this an external redirect that goes to a new origin - let currentUrl = new URL(request.url); - let currentOrigin = currentUrl.origin; - let newOrigin = new URL(location, currentOrigin).origin; - let external = newOrigin !== currentOrigin; + let isAbsolute = + /^[a-z+]+:\/\//i.test(location) || location.startsWith("//"); // Support relative routing in internal redirects - if (!external) { + if (!isAbsolute) { let activeMatches = matches.slice(0, matches.indexOf(match) + 1); let routePathnames = getPathContributingMatches(activeMatches).map( (match) => match.pathnameBase @@ -2642,7 +2640,7 @@ async function callLoaderOrAction( let resolvedLocation = resolveTo( location, routePathnames, - currentUrl.pathname + new URL(request.url).pathname ); invariant( createPath(resolvedLocation), @@ -2673,7 +2671,6 @@ async function callLoaderOrAction( status, location, revalidate: result.headers.get("X-Remix-Revalidate") !== null, - external, }; } diff --git a/packages/router/utils.ts b/packages/router/utils.ts index 53c970f823..9cb192a747 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -41,7 +41,6 @@ export interface RedirectResult { status: number; location: string; revalidate: boolean; - external: boolean; } /** From cf48d873ff9dd12a34a26d008f0b7cb96d5f335d Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 6 Dec 2022 09:46:25 -0500 Subject: [PATCH 2/4] Use createClientSideUrl --- packages/router/router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/router/router.ts b/packages/router/router.ts index 08b866330d..4117203930 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -1609,7 +1609,7 @@ export function createRouter(init: RouterInit): Router { // Check if this an external redirect that goes to a new origin if (typeof window?.location !== "undefined") { let currentOrigin = window.location.origin; - let newOrigin = new URL(redirect.location, currentOrigin).origin; + let newOrigin = createClientSideURL(redirect.location).origin; if (currentOrigin !== newOrigin) { if (replace) { window.location.replace(redirect.location); From 2cc8f46490de75019b0178f149e8a3b5360c8f2d Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 6 Dec 2022 09:48:07 -0500 Subject: [PATCH 3/4] inline origin --- packages/router/router.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/router/router.ts b/packages/router/router.ts index 4117203930..7a830f8712 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -1608,9 +1608,8 @@ export function createRouter(init: RouterInit): Router { // Check if this an external redirect that goes to a new origin if (typeof window?.location !== "undefined") { - let currentOrigin = window.location.origin; let newOrigin = createClientSideURL(redirect.location).origin; - if (currentOrigin !== newOrigin) { + if (window.location.origin !== newOrigin) { if (replace) { window.location.replace(redirect.location); } else { From 55776c48f888a1fd1a75f533bf2080506d38971d Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 6 Dec 2022 09:48:38 -0500 Subject: [PATCH 4/4] Add changeset --- .changeset/swift-lemons-cough.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/swift-lemons-cough.md diff --git a/.changeset/swift-lemons-cough.md b/.changeset/swift-lemons-cough.md new file mode 100644 index 0000000000..90a3ee84a4 --- /dev/null +++ b/.changeset/swift-lemons-cough.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Fix URL creation in Remix integration tests