Next.js Discord

Discord Forum

Cookies are empty in Server Components

Answered
Morelet’s Crocodile posted this in #help-forum
Open in Discord
Morelet’s CrocodileOP
I am experiencing an issue on the dev server (did not yet test on live server).
Whenever I set a cookie, it's not being accessed from RSC pages.

export const dynamic = 'force-dynamic'
export default async function ThanksPage() {
  const t = await getTranslations()

  // Read booking id from secure cookie set on successful submission
  console.log('[PAGE] rendering ThanksPage')
  const cookieStore = await cookies()
  console.log('[PAGE] all cookies:', cookieStore.getAll())
  console.log(
    '[PAGE] booking:',
    cookieStore.get('booking')?.value ?? '(missing)',
  )
  const bookingId = cookieStore.get('booking')?.value

  console.log('Booking ID from cookie (/it/thanks):', bookingId) // LOGS "undefined"

  // rest of the component...)

  return <TheRestOfMyComponent />
}


// Set the cookie
res.cookies.set('booking', String(data.id), {
  httpOnly: true,
  sameSite: 'lax',
  path: '/',
  maxAge: 60 * 15, // 15 minutes
  secure: process.env.NODE_ENV === 'production',
})


proxy.ts is attached due to message length

The cookies are being logged correctly from proxy.ts, and they do appear in the browser's DevTools
They however come out as empty in Server Rendered page (/it/thanks in this case)

Is this an issue on my side?
Answered by Morelet’s Crocodile
Turns out I'm a dumbass: even though I had marked the page as "force-dynamic", its layout had "force-static".
View full answer

3 Replies

Morelet’s CrocodileOP
Checked the browser logs and terminal logs, no errors there
Now checked on the live server as well and it's broken there as well
Morelet’s CrocodileOP
Turns out I'm a dumbass: even though I had marked the page as "force-dynamic", its layout had "force-static".
Answer