Skip to content

fix cookie redirect matcher #261

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

Merged
merged 3 commits into from
Oct 10, 2023
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
8 changes: 8 additions & 0 deletions examples/app-router/app/config-redirect/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function RedirectDestination() {
return (
<div>
<h1>I was redirected from next.config.js</h1>
<p>/next-config-redirect =&gt; /config-redirect</p>
</div>
);
}
34 changes: 34 additions & 0 deletions examples/app-router/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ const nextConfig = {
experimental: {
serverActions: true,
},
redirects: () => {
return [
{
source: "/next-config-redirect-missing",
destination: "/config-redirect?missing=true",
permanent: true,
missing: [{ type: "cookie", key: "missing-cookie" }],
},
{
source: "/next-config-redirect-not-missing",
destination: "/config-redirect?missing=true",
permanent: true,
missing: [{ type: "cookie", key: "from" }], // middleware sets this cookie
},
{
source: "/next-config-redirect-has",
destination: "/config-redirect?has=true",
permanent: true,
has: [{ type: "cookie", key: "from" }],
},
{
source: "/next-config-redirect-has-with-value",
destination: "/config-redirect?hasWithValue=true",
permanent: true,
has: [{ type: "cookie", key: "from", value: "middleware" }],
},
{
source: "/next-config-redirect-has-with-bad-value",
destination: "/config-redirect?hasWithBadValue=true",
permanent: true,
has: [{ type: "cookie", key: "from", value: "wrongvalue" }],
},
];
},
headers() {
return [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/open-next/src/adapters/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const routeHasMatcher =
switch (redirect.type) {
case "header":
return (
headers?.[redirect.key.toLowerCase()] !== "" &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@conico974 Should this be updated too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to be updated as well, but i think we should do something like

headers?.[redirect.key.toLowerCase()] && headers?.[redirect.key.toLowerCase()] !== ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!!headers?.[redirect.key.toLowerCase()] should take care of both those conditions right? undefined and empty string will both be false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@conico974 gonna merge this unless I should make your suggested changes.

!!headers?.[redirect.key.toLowerCase()] &&
new RegExp(redirect.value ?? "").test(
headers[redirect.key.toLowerCase()] ?? "",
)
);
case "cookie":
return (
cookies?.[redirect.key] !== "" &&
!!cookies?.[redirect.key] &&
new RegExp(redirect.value ?? "").test(cookies[redirect.key] ?? "")
);
case "query":
Expand Down
75 changes: 75 additions & 0 deletions packages/tests-e2e/tests/appRouter/config.redirect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { expect, test } from "@playwright/test";
/**
* This tests that the "redirect" config in next.config.js works
*
* redirects: () => {
return [
{
source: "/next-config-redirect",
destination: "/config-redirect",
permanent: true,
missing: [{ type: "cookie", key: "missing-cookie" }],
},
];
},
*/
test.describe("Next Config Redirect", () => {
test("Missing cookies", async ({ page }) => {
await page.goto("/");
await page.goto("/next-config-redirect-missing");

await page.waitForURL(`/config-redirect?missing=true`);

const el = page.getByText("I was redirected from next.config.js", {
exact: true,
});
await expect(el).toBeVisible();
});
test("Not missing cookies", async ({ page }) => {
await page.goto("/");
await page.goto("/next-config-redirect-not-missing");

// the cookie was not missing, so no redirects
await page.waitForURL("/next-config-redirect-not-missing");

const el = page.getByText("This page could not be found.", {
exact: true,
});
await expect(el).toBeVisible();
});
test("Has cookies", async ({ page }) => {
await page.goto("/");
await page.goto("/next-config-redirect-has");

await page.waitForURL(`/config-redirect?has=true`);

const el = page.getByText("I was redirected from next.config.js", {
exact: true,
});
await expect(el).toBeVisible();
});
test("Has cookies with value", async ({ page }) => {
await page.goto("/");
await page.goto("/next-config-redirect-has-with-value");

await page.waitForURL(`/config-redirect?hasWithValue=true`);

const el = page.getByText("I was redirected from next.config.js", {
exact: true,
});
await expect(el).toBeVisible();
});
test("Has cookies with bad value", async ({ page }) => {
await page.goto("/");
await page.goto("/next-config-redirect-has-with-bad-value");

// did not redirect
await page.waitForURL(`/next-config-redirect-has-with-bad-value`);

// 404 not found
const el = page.getByText("This page could not be found.", {
exact: true,
});
await expect(el).toBeVisible();
});
});