This library provides comprehensive cookie management for Next.js 13+ applications, specifically designed for the App Router. It offers solutions for both client-side components (SSR-capable) and server-only components. The current stable version is 2.1.1. While a specific release cadence isn't stated, the package appears actively maintained, indicated by recent updates and explicit migration instructions from v1. Key differentiators include its ability to support client components rendered on the server (SSR) with a special dehydration mechanism for cookie updates, a capability not natively offered by Next.js for server components. Its client-side interface is based on the popular `js-cookie` package, providing a familiar API. It addresses the challenge of handling cookies consistently across the varied rendering environments of Next.js's App Directory.
npm install next-client-cookiesVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up CookiesProvider in `app/layout.tsx` and using the `useCookies` hook in a client component to read, set, and delete cookies.
Apply `CookiesProvider` on a per-page or per-layout basis where cookie functionality is strictly required, instead of the root `app/layout.tsx`.
For cookie updates in server-only components, utilize Next.js Server Actions or Route Handlers. If client-side interaction and SSR updates are needed, ensure the component is a Client Component (`'use client'`).
Update calls from `const cookies = getCookies();` to `const cookies = await getCookies();` in your server components.
Change `const cookies = getCookies();` to `const cookies = await getCookies();`.
Add `'use client';` at the very top of the file where `useCookies` is called, or refactor the cookie logic into a designated Client Component.
Ensure `import { CookiesProvider } from 'next-client-cookies/server';` is present in the file where `CookiesProvider` is used, typically `app/layout.tsx`.