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/red-starfishes-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Fix api rewrite destination with i18n and query not forwarded on rewrite
1 change: 1 addition & 0 deletions examples/pages-router/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const nextConfig: NextConfig = {
],
rewrites: async () => [
{ source: "/rewrite", destination: "/", locale: false },
{ source: "/rewriteWithQuery", destination: "/api/query?q=1" },
{
source: "/rewriteUsingQuery",
destination: "/:destination/",
Expand Down
5 changes: 5 additions & 0 deletions examples/pages-router/src/pages/api/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ query: req.query });
}
15 changes: 14 additions & 1 deletion packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,23 @@ export function handleRewrites<T extends RewriteDefinition>(
rewrittenHost = unescapeRegex(toDestinationHost(params));
rewrittenQuery = unescapeRegex(toDestinationQuery(params));
}

// We need to strip the locale from the path if it's a local api route
if (NextConfig.i18n && !isExternalRewrite) {
const strippedPathLocale = rewrittenPath.replace(
new RegExp(`^/(${NextConfig.i18n.locales.join("|")})`),
"",
);
if (strippedPathLocale.startsWith("/api/")) {
rewrittenPath = strippedPathLocale;
}
}

rewrittenUrl = isExternalRewrite
? `${protocol}//${rewrittenHost}${rewrittenPath}`
: new URL(rewrittenPath, event.url).href;

// Should we merge the query params or use only the ones from the rewrite?
// We merge query params from the source and the destination
finalQuery = {
...query,
...convertFromQueryString(rewrittenQuery),
Expand All @@ -243,6 +255,7 @@ export function handleRewrites<T extends RewriteDefinition>(
return {
internalEvent: {
...event,
query: finalQuery,
rawPath: new URL(rewrittenUrl).pathname,
url: rewrittenUrl,
},
Expand Down
12 changes: 12 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/rewrite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ test("Rewrite to external image", async ({ request }) => {
expect(response.headers()["content-type"]).toBe("image/png");
expect(validateMd5(await response.body(), EXT_PNG_MD5)).toBe(true);
});

test("Rewrite with query in destination", async ({ request }) => {
const response = await request.get("/rewriteWithQuery");
expect(response.status()).toBe(200);
expect(await response.json()).toEqual({ query: { q: "1" } });
});

test("Rewrite with query should merge query params", async ({ request }) => {
const response = await request.get("/rewriteWithQuery?b=2");
expect(response.status()).toBe(200);
expect(await response.json()).toEqual({ query: { q: "1", b: "2" } });
});
4 changes: 4 additions & 0 deletions packages/tests-unit/tests/core/routing/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ describe("handleRewrites", () => {
expect(result).toEqual({
internalEvent: {
...event,
query: {
album: "foo",
song: "bar",
},
rawPath: "/search",
url: "https://external.com/search?album=foo&song=bar",
},
Expand Down
Loading