diff --git a/.changeset/fast-moles-joke.md b/.changeset/fast-moles-joke.md new file mode 100644 index 000000000..448ee131a --- /dev/null +++ b/.changeset/fast-moles-joke.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Fix incorrect redirects to different domains diff --git a/examples/app-router/next.config.js b/examples/app-router/next.config.js index d35b9fc21..8eef18d5e 100644 --- a/examples/app-router/next.config.js +++ b/examples/app-router/next.config.js @@ -51,6 +51,13 @@ const nextConfig = { permanent: true, has: [{ type: "cookie", key: "from", value: "wrongvalue" }], }, + { + source: "/next-config-redirect-without-locale-support", + destination: "https://open-next.js.org/", + permanent: false, + basePath: false, + locale: false, + }, ]; }, headers() { diff --git a/examples/pages-router/next.config.js b/examples/pages-router/next.config.js index a0a5044b8..16724adbd 100644 --- a/examples/pages-router/next.config.js +++ b/examples/pages-router/next.config.js @@ -22,6 +22,15 @@ const nextConfig = { ], }, ], + redirects: () => [ + { + source: "/next-config-redirect-without-locale-support/", + destination: "https://open-next.js.org/", + permanent: false, + basePath: false, + locale: false, + }, + ], trailingSlash: true, }; diff --git a/packages/open-next/src/core/routing/util.ts b/packages/open-next/src/core/routing/util.ts index d2bcb88df..829265f74 100644 --- a/packages/open-next/src/core/routing/util.ts +++ b/packages/open-next/src/core/routing/util.ts @@ -52,13 +52,13 @@ export function getUrlParts(url: string, isExternal: boolean) { }; } - const regex = /^(https?:\/\/)?([^\/\s]+)(\/[^?]*)?(\?.*)?/; + const regex = /^(https?:)\/\/?([^\/\s]+)(\/[^?]*)?(\?.*)?/; const match = url.match(regex); if (!match) { throw new Error(`Invalid external URL: ${url}`); } return { - protocol: match[1] ?? "https://", + protocol: match[1] ?? "https:", hostname: match[2], pathname: match[3], queryString: match[4]?.slice(1) ?? "", diff --git a/packages/tests-e2e/tests/pagesRouter/redirect.test.ts b/packages/tests-e2e/tests/pagesRouter/redirect.test.ts new file mode 100644 index 000000000..2937fd378 --- /dev/null +++ b/packages/tests-e2e/tests/pagesRouter/redirect.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from "@playwright/test"; + +test("Single redirect", async ({ page }) => { + await page.goto("/next-config-redirect-without-locale-support/"); + + await page.waitForURL("https://open-next.js.org/"); + let el = page.getByRole("heading", { name: "Open source Next.js adapter" }); + await expect(el).toBeVisible(); +});