Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export function handleRewrites<T extends RewriteDefinition>(
}
rewrittenUrl = isExternalRewrite
? `${protocol}//${rewrittenHost}${rewrittenPath}`
: `/${rewrittenPath}`;
: `${rewrittenPath}`;
// Should we merge the query params or use only the ones from the rewrite?
finalQuery = {
...query,
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function getUrlParts(url: string, isExternal: boolean) {
const match = url.match(regex);
return {
hostname: "",
pathname: match?.[1] ?? url,
pathname: match?.[1] ? `/${match[1]}` : url,
protocol: "",
queryString: match?.[2] ?? "",
};
Expand All @@ -61,7 +61,7 @@ export function getUrlParts(url: string, isExternal: boolean) {
return {
protocol: match[1] ?? "https:",
hostname: match[2],
pathname: match[3],
pathname: match[3] ?? "",
queryString: match[4]?.slice(1) ?? "",
};
}
Expand Down
30 changes: 24 additions & 6 deletions packages/tests-unit/tests/core/routing/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,31 @@ describe("handleRedirects", () => {
{
source: "/:path+",
destination: "/new/:path+",
internal: true,
locale: false,
statusCode: 308,
regex: "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$",
regex: "^(?!/_next)(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))(?:/)?$",
},
]);

expect(result).toBeUndefined();
expect(result.headers.Location).toBe("/new/api-route");
});

it("should redirect matching nested path", () => {
const event = createEvent({
url: "/api-route/secret",
});

const result = handleRedirects(event, [
{
source: "/:path+",
destination: "/new/:path+",
locale: false,
statusCode: 308,
regex: "^(?!/_next)(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))(?:/)?$",
},
]);

expect(result.headers.Location).toBe("/new/api-route/secret");
});

it("should not redirect unmatched path", () => {
Expand All @@ -258,9 +276,9 @@ describe("handleRedirects", () => {
{
source: "/foo/",
destination: "/bar",
internal: true,
statusCode: 308,
regex: "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))/$",
locale: false,
statusCode: 307,
regex: "^(?!/_next)/foo/(?:/)?$",
},
]);

Expand Down
18 changes: 14 additions & 4 deletions packages/tests-unit/tests/core/routing/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe("getUrlParts", () => {
it("returns url parts for /", () => {
expect(getUrlParts("/", false)).toEqual({
hostname: "",
pathname: "", // TODO: This behaviour is inconsistent with external pathname
pathname: "/",
protocol: "",
queryString: "",
});
Expand All @@ -128,16 +128,16 @@ describe("getUrlParts", () => {
it("returns url parts", () => {
expect(getUrlParts("/relative", false)).toEqual({
hostname: "",
pathname: "relative",
protocol: "", // TODO: This behaviour is inconsistent with external pathname
pathname: "/relative",
protocol: "",
queryString: "",
});
});

it("returns url parts with query string", () => {
expect(getUrlParts("/relative/path?query=1", false)).toEqual({
hostname: "",
pathname: "relative/path", // TODO: This behaviour is inconsistent with external pathname
pathname: "/relative/path",
protocol: "",
queryString: "query=1",
});
Expand All @@ -162,6 +162,16 @@ describe("getUrlParts", () => {
});
});

// For reference https://github.com/opennextjs/opennextjs-aws/issues/591
it("returns url parts for / without trailing slash", () => {
expect(getUrlParts("http://localhost", true)).toEqual({
hostname: "localhost",
pathname: "",
protocol: "http:",
queryString: "",
});
});

it("returns url parts", () => {
expect(getUrlParts("https://localhost/relative", true)).toEqual({
hostname: "localhost",
Expand Down
Loading