Next Cookies is a focused utility library for Next.js applications, providing a straightforward method to read HTTP cookies on both the client and server sides. Currently at version 2.0.3, it offers a stable, minimalistic API centered around a single default export function that takes Next.js's `ctx` object (from `getInitialProps`) as an argument. Its primary differentiator is its simplicity and universal access pattern, allowing developers to consistently retrieve cookie values regardless of whether the page is rendered server-side or client-side. The package explicitly *does not* handle setting or deleting cookies, delegating these operations to native browser APIs like `document.cookie` or other specialized libraries. This clear separation of concerns makes it lightweight and easy to integrate for cookie *reading* tasks within the Next.js ecosystem. Given its mature state and narrow scope, it is likely in a maintenance cadence, receiving updates primarily for compatibility or critical bug fixes.
npm install next-cookiesVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to read cookies using `next-cookies` within Next.js's `getInitialProps` and how to set/delete them using native browser `document.cookie` API.
To set a cookie client-side: `document.cookie = 'name=value; path=/';`. To delete a cookie client-side: `document.cookie = 'name=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT';`. For server-side cookie management, use `res.setHeader('Set-Cookie', ...)` in API routes or `getServerSideProps`.Ensure `cookies(ctx)` is called within `getInitialProps` or a Next.js API route. For client-side components, cookies must be read once in `getInitialProps` and passed as props, or accessed directly via `document.cookie`.
Consider alternative cookie parsing methods if transitioning to the Next.js App Router, such as reading cookies directly from incoming request headers in server components or route handlers.
Change `import { cookies } from "next-cookies"` to `import cookies from "next-cookies"`.Ensure `cookies(ctx)` is invoked within the `static async getInitialProps(ctx)` method of a Next.js page component or inside a Next.js API route handler.
Use native browser APIs like `document.cookie = 'key=value'` for setting cookies and `document.cookie = 'key=; expires=...'` for deleting them. Re-fetch cookies via `next-cookies` on subsequent requests/SSR cycles to see changes.
No dependency data recorded yet.