Gathering detailed insights and metrics for cookies-next
Gathering detailed insights and metrics for cookies-next
Gathering detailed insights and metrics for cookies-next
Gathering detailed insights and metrics for cookies-next
next-cookies
get the cookies on both the client & server
next-client-cookies
SSR and client support for Next.js v13 cookies (app directory)
urllib-next
Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. Base undici fetch API.
@marshallku/cookies-next
Getting, setting and removing cookies on both client and server with next.js
Getting, setting and removing cookies on both client and server with next.js
npm install cookies-next
Typescript
Module System
Node Version
NPM Version
67.1
Supply Chain
81.8
Quality
82.3
Maintenance
100
Vulnerability
91.4
License
TypeScript (99.75%)
JavaScript (0.25%)
Total Downloads
47,661,795
Last Day
18,709
Last Week
545,173
Last Month
2,302,671
Last Year
24,607,715
MIT License
726 Stars
169 Commits
49 Forks
5 Watchers
3 Branches
15 Contributors
Updated on Jul 01, 2025
Minified
Minified + Gzipped
Latest Version
6.0.0
Package Id
cookies-next@6.0.0
Unpacked Size
125.05 kB
Size
22.00 kB
File Count
50
NPM Version
10.2.4
Node Version
20.11.0
Published on
May 27, 2025
Cumulative downloads
Total Downloads
1
A versatile cookie management library for Next.js applications, supporting both client-side and server-side operations.
For Next.js versions 15 and above, use the latest version of cookies-next:
1npm install --save cookies-next@latest
For Next.js versions 12.2.0 to 14.x, use cookies-next version 4.3.0:
1npm install --save cookies-next@4.3.0
For Next.js 15+:
1// For client-side usage 2import { 3 getCookie, 4 getCookies, 5 setCookie, 6 deleteCookie, 7 hasCookie, 8 useGetCookies, 9 useSetCookie, 10 useHasCookie, 11 useDeleteCookie, 12 useGetCookie, 13} from 'cookies-next/client'; 14 15// For server-side usage 16import { getCookie, getCookies, setCookie, deleteCookie, hasCookie } from 'cookies-next/server';
Also, you can leave the decision of which import to use to the the library itself, by importing from the root:
1import { getCookie, getCookies, setCookie, deleteCookie, hasCookie } from 'cookies-next';
For Next.js 12.2.0 to 13.x:
1import { getCookie, getCookies, setCookie, deleteCookie, hasCookie } from 'cookies-next';
1setCookie('key', 'value', options);
1const value = getCookie('key', options);
1const cookies = getCookies(options);
1const exists = hasCookie('key', options);
1deleteCookie('key', options);
Using separate hook for each cookie function:
1'use client'; 2 3import { useGetCookies, useSetCookie, useHasCookie, useDeleteCookie, useGetCookie } from 'cookies-next'; 4 5function ClientComponent() { 6 const setCookie = useSetCookie(); 7 const hasCookie = useHasCookie(); 8 const deleteCookie = useDeleteCookie(); 9 const getCookies = useGetCookies(); 10 const getCookie = useGetCookie(); 11 12 setCookie('key', 'value'); 13 14 return ( 15 <div> 16 <p>hasCookie - {JSON.stringify(hasCookie('key'))}</p> 17 <p>getCookies - {JSON.stringify(getCookies)}</p> 18 <p>getCookie - {getCookie('key')}</p> 19 <button type="button" onClick={() => deleteCookie('key')}> 20 deleteCookie 21 </button> 22 </div> 23 ); 24}
Using one hook that returns all of the cookie functions:
1'use client'; 2 3import { useCookiesNext } from 'cookies-next'; 4 5function ClientComponent() { 6 const { setCookie, hasCookie, deleteCookie, getCookies, getCookie } = useCookiesNext(); 7 8 setCookie('key', 'value'); 9 10 return ( 11 <div> 12 <p>hasCookie - {JSON.stringify(hasCookie('key'))}</p> 13 <p>getCookies - {JSON.stringify(getCookies)}</p> 14 <p>getCookie - {getCookie('key')}</p> 15 <button type="button" onClick={() => deleteCookie('key')}> 16 deleteCookie 17 </button> 18 </div> 19 ); 20}
If you are going to perform actions on cookies inside a useEffect, make sure to add the cookie function returned from the hook to the dependency array.
1const getCookies = useGetCookies(); 2 3useEffect(() => { 4 console.log('getCookies', getCookies()); 5}, [getCookies]);
In cookies-next
, standard hooks are static as they primarily address issues related to hydration in client components (since they are stateless). In contrast, reactive hooks maintain their own React state and require adding the CookiesNextProvider
to your component tree.
Adding a provider:
1"use client"; 2 3import { ReactElement, ReactNode } from "react"; 4import { CookiesNextProvider } from "cookies-next"; 5 6interface ProvidersProps { 7 children: ReactNode; 8} 9 10export function Providers({ children }: ProvidersProps) { 11 return <CookiesNextProvider pollingOptions={{ enabled: true, intervalMs: 1000 }}>{children}</CookiesNextProvider>; 12}
The purpose of polling is to monitor document.cookie and reflect changes made without a cookies-next action, such as when client-side cookies are set by the server. By default, polling is disabled.
Using hooks:
1'use client'; 2 3import { 4 useReactiveGetCookie, 5 useReactiveGetCookies, 6 useReactiveHasCookie, 7 useReactiveSetCookie, 8 useReactiveDeleteCookie, 9} from 'cookies-next'; 10 11const hasCookie = useReactiveHasCookie(); 12const getCookies = useReactiveGetCookies(); 13const getCookie = useReactiveGetCookie(); 14const setCookie = useReactiveSetCookie(); 15const deleteCookie = useReactiveDeleteCookie(); 16 17/* --- */
or:
1'use client'; 2 3import { useReactiveCookiesNext } from 'cookies-next'; 4 5const { hasCookie, getCookies, getCookie, setCookie, deleteCookie } = useReactiveCookiesNext(); 6 7/* --- */
When the cookie state changes, all cookies within the components wrapped with CookiesNextProvider
will be updated in the cookie functions provided by reactive hooks.
1'use client'; 2 3import { getCookies, setCookie, deleteCookie, getCookie } from 'cookies-next/client'; 4 5function ClientComponent() { 6 /* 7 ❗❗❗ In a client component, it's highly recommended to use cookies-next functions within useEffect or in event handlers; otherwise, you might encounter hydration mismatch errors. - 8 https://react.dev/link/hydration-mismatch. 9 */ 10 11 useEffect(() => { 12 getCookies(); 13 getCookie('key'); 14 setCookie('key', 'value'); 15 deleteCookie('key'); 16 hasCookie('key'); 17 }, []); 18 19 const handleClick = () => { 20 getCookies(); 21 getCookie('key'); 22 setCookie('key', 'value'); 23 deleteCookie('key'); 24 hasCookie('key'); 25 }; 26 27 /* .... */ 28}
In Server Components:
1import { getCookie, getCookies, hasCookie } from 'cookies-next/server'; 2import { cookies } from 'next/headers'; 3 4export const ServerComponent = async () => { 5 // Read-only operations in Server Components 6 const value = await getCookie('test', { cookies }); 7 const allCookies = await getCookies({ cookies }); 8 const exists = await hasCookie('test', { cookies }); 9 10 // Note: It's not possible to update cookies in Server Components 11 ❌ await setCookie("test", "value", { cookies }); // Won't work 12 ❌ await deleteCookie('test', { cookies }); // Won't work 13 14 return <div>...</div>; 15};
In Server Actions:
1'use server'; 2 3import { cookies } from 'next/headers'; 4import { setCookie, deleteCookie, getCookie, getCookies, hasCookie } from 'cookies-next/server'; 5 6export async function serverAction() { 7 await setCookie('test', 'value', { cookies }); 8 await deleteCookie('test', { cookies }); 9 await getCookie('test', { cookies }); 10 await getCookies({ cookies }); 11 await hasCookie('test', { cookies }); 12}
1import { cookies } from 'next/headers'; 2import { NextRequest, NextResponse } from 'next/server'; 3import { deleteCookie, getCookie, setCookie, hasCookie, getCookies } from 'cookies-next/server'; 4 5export async function GET(req: NextRequest) { 6 const res = new NextResponse(); 7 await setCookie('test', 'value', { res, req }); 8 await getCookie('test', { res, req }); 9 await getCookies({ res, req }); 10 await deleteCookie('test', { res, req }); 11 await hasCookie('test', { req, res }); 12 13 // Using cookies function 14 await setCookie('test1', 'value', { cookies }); 15 await getCookie('test1', { cookies }); 16 await getCookies({ cookies }); 17 await deleteCookie('test1', { cookies }); 18 await hasCookie('test1', { cookies }); 19 20 return res; 21}
1import { NextResponse } from 'next/server'; 2import type { NextRequest } from 'next/server'; 3import { getCookie, setCookie, deleteCookie, hasCookie, getCookies } from 'cookies-next/server'; 4 5export async function middleware(req: NextRequest) { 6 const res = NextResponse.next(); 7 await setCookie('test', 'value', { res, req }); 8 await hasCookie('test', { req, res }); 9 await deleteCookie('test', { res, req }); 10 await getCookie('test', { res, req }); 11 await getCookies({ res, req }); 12 13 return res; 14}
Sets a cookie.
1setCookie('key', 'value', options);
Retrieves a specific cookie.
1const value = getCookie('key', options);
Retrieves all cookies.
1const cookies = getCookies(options);
Checks if a cookie exists.
1const exists = hasCookie('key', options);
Deletes a cookie.
1deleteCookie('key', options);
req
: Required for server-side operations (except when using cookies
function)res
: Required for server-side operations (except when using cookies
function)cookies
: Function from next/headers
, used in App Router for server-side operationsdomain
: Specifies the cookie's domainpath
: Specifies the cookie's pathmaxAge
: Specifies the cookie's maximum age in secondshttpOnly
: Sets the HttpOnly flagsecure
: Sets the Secure flagsameSite
: Sets the SameSite attribute ('strict', 'lax', or 'none')For more detailed options, refer to the cookie specification.
MIT
No vulnerabilities found.
Reason
19 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 2/6 approved changesets -- score normalized to 3
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreLast Day
3.3%
18,709
Compared to previous day
Last Week
-4.7%
545,173
Compared to previous week
Last Month
-0.2%
2,302,671
Compared to previous month
Last Year
65.8%
24,607,715
Compared to previous year