Skip to content

Commit

Permalink
Fix issue where an incoming query string got lost in a hash redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Sep 11, 2024
1 parent 8acab3b commit b8c114e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/pages-shared/__tests__/asset-server/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,21 @@ describe("asset-server handler", () => {
"https://foobar.com/#def?test=abc"
);
});

// Query string needs to be _before_ the hash
test("redirects to a hash with an incoming query cross-origin", async () => {
const { response } = await getTestResponse({
request: "https://foo.com/bar?test=abc",
metadata: createMetadataObjectWithRedirects([
{ from: "/bar", to: "https://foobar.com/#heading", status: 301 },
]),
});

expect(response.status).toBe(301);
expect(response.headers.get("Location")).toBe(
"https://foobar.com/?test=abc#heading"
);
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/pages-shared/asset-server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ export async function generateHandler<
? `${destination.pathname}${destination.search || search}${
destination.hash
}`
: `${destination.href}${destination.search ? "" : search}`;
: `${destination.href.slice(0, -(destination.search.length + destination.hash.length))}${
destination.search ? "" : search
}${destination.hash}`;

switch (status) {
case 301:
Expand Down

0 comments on commit b8c114e

Please sign in to comment.