cookies-next is a specialized library designed for managing HTTP cookies within Next.js applications, supporting operations on both the client and server sides, including Server Components and API routes. The current stable version is 6.1.1. It maintains a relatively active release cadence, frequently updating to support new Next.js versions and introduce features like React hooks for client-side state management, including 'reactive hooks' that trigger component re-renders when cookie values change. Key differentiators include its explicit support for Next.js's distinct client and server environments, offering separate import paths for optimized bundles, and providing both static and reactive hooks for flexible client-side usage, distinguishing it from general-purpose cookie libraries.
npm install cookies-nextVerified import paths — ran on the pinned version, not inferred.
Demonstrates server-side cookie management within a Next.js App Router API route, including setting, getting, and deleting cookies. It shows how to integrate with Next.js's native `cookies` function for server contexts.
For Next.js 15+, update imports to `from 'cookies-next/client'` for client components and `from 'cookies-next/server'` for server components/API routes. Alternatively, use `from 'cookies-next'` but be aware of potential bundle size implications for older Next.js versions.
Review components using client-side cookie hooks (`useGetCookies`, `useGetCookie`, etc.) after upgrading to v6.0.0. If you relied on the static behavior, you might experience unexpected re-renders. Consider using reactive hooks where explicit re-rendering is desired or adjust component logic.
Ensure you import `cookies` from `next/headers` and pass `{ cookies }` as an option to `setCookie` or `deleteCookie` when operating in Server Components or API routes (e.g., `setCookie('key', 'value', { cookies });`).Wrap your application or relevant client components with `CookiesNextProvider` to enable the reactive functionality of hooks like `useCookiesNext` or those imported from `cookies-next/client`.
Import `cookies` from `next/headers` and pass it as an option: `const myCookie = getCookie('mykey', { cookies: cookies() });`Ensure that server-side cookie operations are performed within a `getServerSideProps`, API route, or Server Component where `req`/`res` or the `cookies` object from `next/headers` are accessible and passed to the functions as options.
Use client-side hooks within components marked with 'use client' and ensure any cookie-dependent rendering logic is robustly handled or deferred until the client-side. For SSR, ensure that server-set cookies are consistent or handle variations gracefully.