Next.js App Router Cookie Management
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.
Common errors
-
TypeError: cookies.get is not a function
cause The `getCookies()` function in a Server Component was called without `await`, causing `cookies` to be a Promise object instead of the resolved cookie interface.fixChange `const cookies = getCookies();` to `const cookies = await getCookies();`. -
Error: useCookies() must be used within a Client Component.
cause Attempting to call the `useCookies` hook inside a Server Component (a file without the `'use client'` directive at the top).fixAdd `'use client';` at the very top of the file where `useCookies` is called, or refactor the cookie logic into a designated Client Component. -
ReferenceError: CookiesProvider is not defined
cause The `CookiesProvider` component was used without being correctly imported or with an incorrect import path.fixEnsure `import { CookiesProvider } from 'next-client-cookies/server';` is present in the file where `CookiesProvider` is used, typically `app/layout.tsx`.
Warnings
- gotcha Placing `CookiesProvider` in `app/layout.tsx` will cause the entire application to opt out of static generation. Consider using it only on specific pages or nested layouts that require dynamic cookie access to maintain static generation for other parts of your app.
- gotcha Next.js Server Components inherently do not support directly updating cookies outside of Actions or Route Handlers. While `next-client-cookies` offers a dehydration mechanism for client components rendered via SSR to update cookies, server-only components remain restricted to Next.js's native methods for server-side cookie modification.
- breaking When migrating from v1, the `getCookies` function is now asynchronous and must be `await`ed.
Install
-
npm install next-client-cookies -
yarn add next-client-cookies -
pnpm add next-client-cookies
Imports
- CookiesProvider
import { CookiesProvider } from 'next-client-cookies';import { CookiesProvider } from 'next-client-cookies/server'; - useCookies
const { useCookies } = require('next-client-cookies');import { useCookies } from 'next-client-cookies'; - getCookies
const getCookies = require('next-client-cookies/server').getCookies;import { getCookies } from 'next-client-cookies/server';
Quickstart
'use client';
import { useCookies } from 'next-client-cookies';
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<CookiesProvider>{children}</CookiesProvider>
</body>
</html>
);
}
// In a client component (e.g., app/my-component.tsx)
// Make sure your component file starts with 'use client';
const MyCookieComponent = () => {
const cookies = useCookies();
return (
<div>
<p>My cookie value: {cookies.get('my-cookie') ?? 'Not set'}</p>
<button onClick={() => cookies.set('my-cookie', 'my-value', { expires: 7 })}>
Set 'my-cookie'
</button>
{' | '}
<button onClick={() => cookies.remove('my-cookie')}>Delete 'my-cookie'</button>
</div>
);
};