Skip to content

Commit 1b6eba2

Browse files
committed
e2e
1 parent 5a75a31 commit 1b6eba2

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

.changeset/breezy-adults-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: Ensure cookies set in middleware are available on initial render when using `cookies().get()` from Next.js
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { cookies } from "next/headers";
2+
3+
export default async function Page() {
4+
const foo = (await cookies()).get("foo")?.value;
5+
6+
return <div data-testid="foo">{foo}</div>;
7+
}

examples/app-router/middleware.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export function middleware(request: NextRequest) {
2828
const u = new URL("https://opennext.js.org/share.png");
2929
return NextResponse.rewrite(u);
3030
}
31+
if (path === "/cookies") {
32+
const res = NextResponse.next();
33+
res.cookies.set("foo", "bar");
34+
return res;
35+
}
3136
const requestHeaders = new Headers(request.headers);
3237
// Setting the Request Headers, this should be available in RSC
3338
requestHeaders.set("request-header", "request-header");
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import { expect, test } from "@playwright/test";
22

3-
test("Cookies", async ({ page, context }) => {
4-
await page.goto("/");
3+
test.describe("Middleware Cookies", () => {
4+
test("should be able to set cookies on response in middleware", async ({
5+
page,
6+
context,
7+
}) => {
8+
await page.goto("/");
59

6-
const cookies = await context.cookies();
7-
const from = cookies.find(({ name }) => name === "from");
8-
expect(from?.value).toEqual("middleware");
10+
const cookies = await context.cookies();
11+
const from = cookies.find(({ name }) => name === "from");
12+
expect(from?.value).toEqual("middleware");
913

10-
const love = cookies.find(({ name }) => name === "with");
11-
expect(love?.value).toEqual("love");
14+
const love = cookies.find(({ name }) => name === "with");
15+
expect(love?.value).toEqual("love");
16+
});
17+
test("should be able to get cookies set in the middleware with Next's cookies().get()", async ({
18+
page,
19+
}) => {
20+
await page.goto("/cookies");
21+
22+
expect(await page.getByTestId("foo").textContent()).toBe("bar");
23+
});
1224
});

0 commit comments

Comments
 (0)