Cookies

The cookies function allows you to read the HTTP incoming request cookies from a server component. It is read-only, meaning you cannot set / delete the outgoing request cookies.

Note: The Next.js team at Vercel is working on adding the ability to set cookies in addition to the cookies function.

cookies().get(name)

A method that takes a cookie name and returns an object with name and value. If a cookie with name isn't found, it returns undefined. If multiple cookies match, it will only return the first match.

app/page.js
import { cookies } from 'next/headers';

export default function Page() {
  const nextCookies = cookies();
  const theme = nextCookies.get('theme');
  return '...'
}

cookies().getAll()

A method that is similar to get, but returns a list of all the cookies with a matching name. If name is unspecified, it returns all the available cookies.

app/page.js
import { cookies } from 'next/headers';

export default function Page() {
  const nextCookies = cookies();
  return nextCookies.getAll().map((cookie) => (
    <div key={cookie.name}>
      <p>Name: {cookie.name}</p>
      <p>Value: {cookie.value}</p>
    </div>
  ));
}

cookies().has(name)

A method that takes a cookie name and returns a boolean based on if the cookie exists (true) or not (false).

app/page.js
import { cookies } from 'next/headers'

export default function Page() {
  const cookiesList = cookies()
  const hasCookie = cookiesList.has('theme')
  return '...'
}