From f6dc0046d970fbea0e77a70b9788d30facaf9da0 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 2 Feb 2023 11:26:44 -0500 Subject: [PATCH] Only check for differing origin on absolute URL redirects --- .changeset/thirty-frogs-wave.md | 5 +++++ packages/router/router.ts | 14 +++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .changeset/thirty-frogs-wave.md diff --git a/.changeset/thirty-frogs-wave.md b/.changeset/thirty-frogs-wave.md new file mode 100644 index 0000000000..680abd5e68 --- /dev/null +++ b/.changeset/thirty-frogs-wave.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Only check for differing origin on absolute URL redirects diff --git a/packages/router/router.ts b/packages/router/router.ts index 1f56b629e1..6bdd3ef9b1 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -621,6 +621,8 @@ export const IDLE_BLOCKER: BlockerUnblocked = { location: undefined, }; +const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i; + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined" && @@ -1915,8 +1917,12 @@ export function createRouter(init: RouterInit): Router { "Expected a location on the redirect navigation" ); - // Check if this an external redirect that goes to a new origin - if (isBrowser && typeof window?.location !== "undefined") { + // 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" + ) { let newOrigin = init.history.createURL(redirect.location).origin; if (window.location.origin !== newOrigin) { if (replace) { @@ -3093,10 +3099,8 @@ async function callLoaderOrAction( "Redirects returned/thrown from loaders/actions must have a Location header" ); - let isAbsolute = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(location); - // Support relative routing in internal redirects - if (!isAbsolute) { + if (!ABSOLUTE_URL_REGEX.test(location)) { let activeMatches = matches.slice(0, matches.indexOf(match) + 1); let routePathnames = getPathContributingMatches(activeMatches).map( (match) => match.pathnameBase