Sending data from middleware to server component
Unanswered
Burmese posted this in #help-forum
BurmeseOP
if (path === '/') {
request.cookies.set('name', 'lee');
const response = NextResponse.next()
return response;
}setting cookie in middleware like above and then accessing it in server component returning null, why?
const name = cookieStore.get('name')?.value ?? null
console.log(name)Following docs: https://nextjs.org/docs/app/building-your-application/routing/middleware#using-cookies
10 Replies
@Burmese
if (path === '/') {
request.cookies.set('name', 'lee');
const response = NextResponse.next()
return response;
}
setting cookie in middleware like above and then accessing it in server component returning null, why?
const name = cookieStore.get('name')?.value ?? null
console.log(name)
Following docs: https://nextjs.org/docs/app/building-your-application/routing/middleware#using-cookies
if (path === '/') {
const response = NextResponse.next()
response.cookies.set('name', 'lee');
return response;
}you should set the cookie in response instead of request
@Ray ts
if (path === '/') {
const response = NextResponse.next()
response.cookies.set('name', 'lee');
return response;
}
you should set the cookie in response instead of request
BurmeseOP
I want to send data in the server component and docs says that we can set on request also, but that's not working, Idk why?
@Burmese I want to send data in the server component and docs says that we can set on request also, but that's not working, Idk why?
where do you see the doc set on request?
BurmeseOP
See: https://nextjs.org/docs/app/building-your-application/routing/middleware#using-cookies
It says in the 1st point "For incoming requests, cookies comes with the following methods: get, getAll, set, and delete cookies. You can check for the existence of a cookie with has or remove all cookies with clear."
So, for incoming request, we can call set method on it!
It says in the 1st point "For incoming requests, cookies comes with the following methods: get, getAll, set, and delete cookies. You can check for the existence of a cookie with has or remove all cookies with clear."
So, for incoming request, we can call set method on it!
i think it mean the cookies contain those method
see the example, they setting it on response
@Ray i think it mean the cookies contain those method
BurmeseOP
Yes, in the response we can set but docs also says that for the incoming request it is also available so I guess we can use that set method and set cookies in the middleware itself before it actually goes to the route but its not working out.
this is not how it work, if you want to set the cookies on client browser, you have to set it with response
@Ray this is not how it work, if you want to set the cookies on client browser, you have to set it with response
BurmeseOP
No I want to send data from middleware to server component with the help of cookies (as there is no other way I guess other than sending data in headers!)
Alaska pollock
I am stuck with similar problem with integrating next server components with pocketbase authentication. The only method I found is to use headers...