Skip to content

Commit

Permalink
Make sure cookies set in middleware are available (vercel/next.js#49442)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilsn committed Mar 26, 2024
1 parent b903a81 commit 0be5e11
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
21 changes: 21 additions & 0 deletions integrations/evaluations/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { User } from "@pubpub/sdk";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { client } from "./lib/pubpub";
import { RequestCookies, ResponseCookies } from "next/dist/server/web/spec-extension/cookies";

// Makes sure cookies are available on the first request (see
// https://github.com/vercel/next.js/issues/49442#issuecomment-1679807704)
const applySetCookie = (req: NextRequest, res: NextResponse): void => {
// parse the outgoing Set-Cookie header
const setCookies = new ResponseCookies(res.headers);
// Build a new Cookie header for the request by adding the setCookies
const newReqHeaders = new Headers(req.headers);
const newReqCookies = new RequestCookies(newReqHeaders);
setCookies.getAll().forEach((cookie) => newReqCookies.set(cookie));
// set “request header overrides” on the outgoing response
NextResponse.next({
request: { headers: newReqHeaders },
}).headers.forEach((value, key) => {
if (key === "x-middleware-override-headers" || key.startsWith("x-middleware-request-")) {
res.headers.set(key, value);
}
});
};

export default async function middleware(request: NextRequest) {
const response = NextResponse.next();
Expand Down Expand Up @@ -39,6 +59,7 @@ export default async function middleware(request: NextRequest) {

response.cookies.set("instanceId", instanceId);

applySetCookie(request, response);
return response;
}

Expand Down
22 changes: 22 additions & 0 deletions integrations/submissions/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { User } from "@pubpub/sdk";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { client } from "./lib/pubpub";
import { ResponseCookies, RequestCookies } from "next/dist/server/web/spec-extension/cookies";

// Makes sure cookies are available on the first request (see
// https://github.com/vercel/next.js/issues/49442#issuecomment-1679807704)
const applySetCookie = (req: NextRequest, res: NextResponse): void => {
// parse the outgoing Set-Cookie header
const setCookies = new ResponseCookies(res.headers);
// Build a new Cookie header for the request by adding the setCookies
const newReqHeaders = new Headers(req.headers);
const newReqCookies = new RequestCookies(newReqHeaders);
setCookies.getAll().forEach((cookie) => newReqCookies.set(cookie));
// set “request header overrides” on the outgoing response
NextResponse.next({
request: { headers: newReqHeaders },
}).headers.forEach((value, key) => {
if (key === "x-middleware-override-headers" || key.startsWith("x-middleware-request-")) {
res.headers.set(key, value);
}
});
};

export default async function middleware(request: NextRequest) {
const response = NextResponse.next();
Expand Down Expand Up @@ -39,6 +59,8 @@ export default async function middleware(request: NextRequest) {

response.cookies.set("instanceId", instanceId);

applySetCookie(request, response);

return response;
}

Expand Down

0 comments on commit 0be5e11

Please sign in to comment.