Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix absolute redirect detection #9689

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-lemons-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix URL creation in Remix integration tests
32 changes: 14 additions & 18 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines -1609 to -1619
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got rid of the redirect.external flag in favor of just doing the external detection here in client-side only code.

}

// There's no need to abort on redirects, since we don't detect the
Expand Down Expand Up @@ -2627,22 +2627,19 @@ 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the problem - since the origin for a test://test.com URL used in Remix integration tests is null 😕 , and new URL(path, "null") throws an error.

Screenshot 2022-12-06 at 9 32 36 AM

let external = newOrigin !== currentOrigin;
let isAbsolute =
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead we'll just look for the presence of a leading protocol or a protocol-less URL to determine if we can skip relative routing logic


// 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
);
let resolvedLocation = resolveTo(
location,
routePathnames,
currentUrl.pathname
new URL(request.url).pathname
);
invariant(
createPath(resolvedLocation),
Expand Down Expand Up @@ -2673,7 +2670,6 @@ async function callLoaderOrAction(
status,
location,
revalidate: result.headers.get("X-Remix-Revalidate") !== null,
external,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export interface RedirectResult {
status: number;
location: string;
revalidate: boolean;
external: boolean;
}

/**
Expand Down