Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/redirect protocol #451

Merged
merged 8 commits into from
Jun 28, 2024
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/fast-moles-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Fix incorrect redirects to different domains
7 changes: 7 additions & 0 deletions examples/app-router/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions examples/pages-router/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

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 @@ -52,13 +52,13 @@ export function getUrlParts(url: string, isExternal: boolean) {
};
}

const regex = /^(https?:\/\/)?([^\/\s]+)(\/[^?]*)?(\?.*)?/;
const regex = /^(https?:)\/\/?([^\/\s]+)(\/[^?]*)?(\?.*)?/;
JanStevens marked this conversation as resolved.
Show resolved Hide resolved
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) ?? "",
Expand Down
9 changes: 9 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/redirect.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Loading