From bd581426cb046846e817737f678c6eaa5147bb1f Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 6 Dec 2022 11:33:35 -0500 Subject: [PATCH] Fix absolute redirect detection (#9689) * Fix absolute redirect detection * Use createClientSideUrl * inline origin * Add changeset --- .changeset/swift-lemons-cough.md | 5 +++++ packages/router/router.ts | 32 ++++++++++++++------------------ packages/router/utils.ts | 1 - 3 files changed, 19 insertions(+), 19 deletions(-) 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 diff --git a/packages/router/router.ts b/packages/router/router.ts index 8bd5ae9f55..7a830f8712 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -1606,17 +1606,17 @@ 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 newOrigin = createClientSideURL(redirect.location).origin; + if (window.location.origin !== 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 +2627,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 +2639,7 @@ async function callLoaderOrAction( let resolvedLocation = resolveTo( location, routePathnames, - currentUrl.pathname + new URL(request.url).pathname ); invariant( createPath(resolvedLocation), @@ -2673,7 +2670,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; } /**