Skip to content

Commit

Permalink
preserve search/hash in processed redirects (#9489)
Browse files Browse the repository at this point in the history
* preserve search/hash in redirects

* bundle bump
  • Loading branch information
brophdawg11 authored Oct 21, 2022
1 parent ea7351a commit 7dc7f35
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
},
"filesize": {
"packages/router/dist/router.js": {
"none": "106 kB"
"none": "108 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "12.5 kB"
Expand Down
55 changes: 55 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5350,6 +5350,61 @@ describe("a router", () => {
errors: null,
});
});

it("preserves query and hash in redirects", async () => {
let t = setup({ routes: REDIRECT_ROUTES });

let nav1 = await t.fetch("/parent/child", {
formMethod: "post",
formData: createFormData({}),
});

let nav2 = await nav1.actions.child.redirectReturn(
"/parent?key=value#hash"
);
await nav2.loaders.parent.resolve("PARENT");
expect(t.router.state).toMatchObject({
location: {
pathname: "/parent",
search: "?key=value",
hash: "#hash",
},
navigation: IDLE_NAVIGATION,
loaderData: {
parent: "PARENT",
},
errors: null,
});
});

it("preserves query and hash in relative redirects", async () => {
let t = setup({ routes: REDIRECT_ROUTES });

let nav1 = await t.fetch("/parent/child", {
formMethod: "post",
formData: createFormData({}),
});

let nav2 = await nav1.actions.child.redirectReturn(
"..?key=value#hash",
undefined,
undefined,
["parent"]
);
await nav2.loaders.parent.resolve("PARENT");
expect(t.router.state).toMatchObject({
location: {
pathname: "/parent",
search: "?key=value",
hash: "#hash",
},
navigation: IDLE_NAVIGATION,
loaderData: {
parent: "PARENT",
},
errors: null,
});
});
});

describe("scroll restoration", () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2561,18 +2561,21 @@ async function callLoaderOrAction(
(match) => match.pathnameBase
);
let requestPath = createURL(request.url).pathname;
location = resolveTo(location, routePathnames, requestPath).pathname;
let resolvedLocation = resolveTo(location, routePathnames, requestPath);
invariant(
location,
createPath(resolvedLocation),
`Unable to resolve redirect location: ${result.headers.get("Location")}`
);

// Prepend the basename to the redirect location if we have one
if (basename) {
let path = createURL(location).pathname;
location = path === "/" ? basename : joinPaths([basename, path]);
let path = resolvedLocation.pathname;
resolvedLocation.pathname =
path === "/" ? basename : joinPaths([basename, path]);
}

location = createPath(resolvedLocation);

// Don't process redirects in the router during static requests requests.
// Instead, throw the Response and let the server handle it with an HTTP
// redirect. We also update the Location header in place in this flow so
Expand Down

0 comments on commit 7dc7f35

Please sign in to comment.