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
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