Accessing middleware cookies in serversideprops
Unanswered
Singapura posted this in #help-forum
SingapuraOP
I am struggling to access cookies created in middleware from serversideprops. Any idea?
As a workaround, I added the values to response cookies and request header in middleware. And read the values from request header in serversideprops. (See the below example code)
But in this case, the cookies are not forwarded to client. I couldn't find how to pass the request headers with res object
As a workaround, I added the values to response cookies and request header in middleware. And read the values from request header in serversideprops. (See the below example code)
But in this case, the cookies are not forwarded to client. I couldn't find how to pass the request headers with res object
export async function middleware(req: NextRequest) {
let requestHeaders = new Headers(req.headers)
requestHeaders.set('x-test', 'test');
const res = NextResponse.next();
res.cookies.set('test2', 'value');
return NextResponse.next({
request: {
headers: requestHeaders,
},
})