Skip to content
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/itchy-phones-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Respect `replace` behavior on external redirects
38 changes: 37 additions & 1 deletion packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5570,7 +5570,7 @@ describe("a router", () => {
});
});

it("processes external redirects if window is present", async () => {
it("processes external redirects if window is present (push)", async () => {
let urls = [
"http://remix.run/blog",
"https://remix.run/blog",
Expand All @@ -5583,6 +5583,7 @@ describe("a router", () => {
// https://stackoverflow.com/a/60697570
let oldLocation = window.location;
const location = new URL(window.location.href) as unknown as Location;
location.assign = jest.fn();
location.replace = jest.fn();
delete (window as any).location;
window.location = location as unknown as Location;
Expand All @@ -5594,8 +5595,43 @@ describe("a router", () => {
formData: createFormData({}),
});

await A.actions.child.redirectReturn(url);
expect(window.location.assign).toHaveBeenCalledWith(url);
expect(window.location.replace).not.toHaveBeenCalled();

window.location = oldLocation;
}
});

it("processes external redirects if window is present (replace)", async () => {
let urls = [
"http://remix.run/blog",
"https://remix.run/blog",
"//remix.run/blog",
"app://whatever",
];

for (let url of urls) {
// This is gross, don't blame me, blame SO :)
// https://stackoverflow.com/a/60697570
let oldLocation = window.location;
const location = new URL(window.location.href) as unknown as Location;
location.assign = jest.fn();
location.replace = jest.fn();
delete (window as any).location;
window.location = location as unknown as Location;

let t = setup({ routes: REDIRECT_ROUTES });

let A = await t.navigate("/parent/child", {
formMethod: "post",
formData: createFormData({}),
replace: true,
});

await A.actions.child.redirectReturn(url);
expect(window.location.replace).toHaveBeenCalledWith(url);
expect(window.location.assign).not.toHaveBeenCalled();

window.location = oldLocation;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,11 @@ export function createRouter(init: RouterInit): Router {
typeof window !== "undefined" &&
typeof window.location !== "undefined"
) {
window.location.replace(redirect.location);
if (replace) {
window.location.replace(redirect.location);
} else {
window.location.assign(redirect.location);
}
return;
}

Expand Down