liveBook · Manning

Cookies-next

As explained in React in Depth

cookies-next is a utility library for reading and writing cookies in Next.js applications on the client and server.

cookies-next provides a compact client/server cookie API for Next.js. Cookies suit small preferences that must survive reloads and remain available during server rendering, though each request carries their data and storage is smaller than localStorage. section 11.3.3

Use the library with these implementation constraints:

  • Read and write cookies in the browser from anywhere; server reads require the request context, while server writes require returning the appropriate response header. section 11.3.3
  • Store small settings such as temperature scale and time format, rather than sizeable application data suited to localStorage. section 11.3.3

Reading format preferences in a server-rendered route

Import getCookie from cookies-next, read nextjs-isCelsius and nextjs-hour12 with getCookie(name, context), then normalize their string values into boolean defaults before rendering.

How React in Depth covers it

How deep it goes
Explains client/server reading and writing with request context, but does not descend into library internals.
Examples
Uses temperature-scale and time-format cookies in a city route, plus a server-side cookie utility.
What it assumes
Assumes Next.js routing, request context, server rendering, and basic cookie persistence concepts.
What you can do after
Apply cookies-next to read preferences on the server and set or read them in the browser.
How it connects
Connects cookies with Next.js, localStorage, server-side rendering, and hydration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.