Gathering detailed insights and metrics for @marshallku/cookies-next
Gathering detailed insights and metrics for @marshallku/cookies-next
npm install @marshallku/cookies-next
Typescript
Module System
Node Version
NPM Version
72.4
Supply Chain
98.4
Quality
75.9
Maintenance
50
Vulnerability
100
License
TypeScript (99.14%)
Shell (0.86%)
Total Downloads
39,795
Last Day
2
Last Week
12
Last Month
63
Last Year
440
2 Stars
126 Commits
1 Watching
1 Branches
8 Contributors
Minified
Minified + Gzipped
Latest Version
1.3.1
Package Id
@marshallku/cookies-next@1.3.1
Unpacked Size
32.09 kB
Size
8.60 kB
File Count
23
NPM Version
9.8.1
Node Version
18.18.2
Publised On
31 Oct 2023
Cumulative downloads
Total Downloads
Last day
-87.5%
2
Compared to previous day
Last week
-47.8%
12
Compared to previous week
Last month
70.3%
63
Compared to previous month
Last year
-98.9%
440
Compared to previous year
2
5
Successor of cookies-next.
Re-created this repository for faster development of Albamon MSA
Getting, setting and removing cookies with NEXT.JS
npm install --save @marshallku/cookies-next
If you are using next.js version greater than 12.2.0
you need to use cookies-next version 2.1.0
or later
Create a cookie:
1import { setCookie } from "cookies-next"; 2 3setCookie("key", "value", options);
Read a cookie:
1import { getCookie } from "cookies-next"; 2 3getCookie("key", options); // => 'value' 4getCookie("nothing", options); // => undefined
Read all cookies:
1import { getCookies } from "cookies-next"; 2 3getCookies(options); // => { 'name1': 'value1', name2: 'value2' }
Check if a cookie exists:
1import { hasCookie } from "cookies-next"; 2 3hasCookie("name", options); // => true 4hasCookie("nothing", options); // => false
Delete a cookie:
1import { deleteCookie } from "cookies-next"; 2 3deleteCookie(name, options);
IMPORTANT! When deleting a cookie and you're not relying on the default attributes, you must pass the exact same path and domain attributes that were used to set the cookie:
1import { deleteCookie } from "cookies-next"; 2 3deleteCookie(name, { path: "/path", domain: ".yourdomain.com" });
The time complexity of all operations is linear with the number of cookies.
For example, under the hood, getCookie
calls getCookies
. When working reading multiple cookies,
it is fastest to use getCookies
and inspect the returned object.
If you pass ctx (Next.js context) in function, then this function will be done on both client and server
If the function should be done only on client or can't get ctx, pass null or {} as the first argument to the function and when server side rendering, this function return undefined;
1import { getCookies, setCookie, deleteCookie } from "cookies-next"; 2 3// we can use it anywhere 4getCookies(); 5getCookie("key"); 6setCookie("key", "value"); 7deleteCookie("key");
/page/index.js
1import React from "react"; 2import { getCookies, getCookie, setCookie, deleteCookie } from "cookies-next"; 3 4const Home = () => { 5 return <div>page content</div>; 6}; 7 8export const getServerSideProps = ({ req, res }) => { 9 setCookie("test", "value", { req, res, maxAge: 60 * 6 * 24 }); 10 getCookie("test", { req, res }); 11 getCookies({ req, res }); 12 deleteCookie("test", { req, res }); 13 14 return { props: {} }; 15}; 16 17export default Home;
/page/api/example.js
1import type { NextApiRequest, NextApiResponse } from "next"; 2import { getCookies, getCookie, setCookie, deleteCookie } from "cookies-next"; 3 4export default async function handler(req, res) { 5 setCookie("server-key", "value", { req, res, maxAge: 60 * 60 * 24 }); 6 getCookie("key", { req, res }); 7 getCookies({ req, res }); 8 deleteCookie("key", { req, res }); 9 10 return res.status(200).json({ message: "ok" }); 11}
1setCookie("key", "value", options); 2 3setCookie("key", "value"); // - client side 4setCookie("key", "value", { req, res }); // - server side
1getCookies(); // - client side 2getCookies({ req, res }); // - server side
1getCookie('key'); - client side 2getCookie('key', { req, res }); - server side
1hasCookie("key"); // - client side 2hasCookie("key", { req, res }); // - server side
1deleteCookie("key"); // - client side 2deleteCookie("key", { req, res }); // - server side
IMPORTANT! When deleting a cookie and you're not relying on the default attributes, you must pass the exact same path and domain attributes that were used to set the cookie:
1deleteCookie(ctx, name, { path: '/path', domain: '.yourdomain.com' }); - client side
2deleteCookie(ctx, name, { req, res, path: '/path', domain: '.yourdomain.com' }); - server side
cookie's name
cookie's value
required for server side cookies (API and getServerSideProps)
required for server side cookies (API and getServerSideProps)
Specifies the value for the Domain
Set-Cookie
attribute. By default, no
domain is set, and most clients will consider the cookie to apply to only the current domain.
Specifies a function that will be used to encode a cookie's value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value.
The default function is the global encodeURIComponent
, which will encode a JavaScript string
into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range.
A Date
object indicating the cookie's expiration date
By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application.
note the cookie storage model specification states that if both expires
and
maxAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
Specifies the boolean
value for the HttpOnly
Set-Cookie
attribute. When truthy,
the HttpOnly
attribute is set, otherwise it is not. By default, the HttpOnly
attribute is not set.
note be careful when setting this to true
, as compliant clients will not allow client-side
JavaScript to see the cookie in document.cookie
.
Specifies the number
(in seconds) to be the value for the Max-Age
Set-Cookie
attribute.
The given number will be converted to an integer by rounding down. By default, no maximum age is set.
note the cookie storage model specification states that if both expires
and
maxAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
Specifies the value for the Path
Set-Cookie
attribute. By default, the path
is considered the "default path".
Specifies the boolean
or string
to be the value for the SameSite
Set-Cookie
attribute.
true
will set the SameSite
attribute to Strict
for strict same site enforcement.false
will not set the SameSite
attribute.'lax'
will set the SameSite
attribute to Lax
for lax same site enforcement.'none'
will set the SameSite
attribute to None
for an explicit cross-site cookie.'strict'
will set the SameSite
attribute to Strict
for strict same site enforcement.More information about the different enforcement levels can be found in the specification.
note This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
Specifies the boolean
value for the Secure
Set-Cookie
attribute. When truthy,
the Secure
attribute is set, otherwise it is not. By default, the Secure
attribute is not set.
note be careful when setting this to true
, as compliant clients will not send the cookie back to
the server in the future if the browser does not have an HTTPS connection.
MIT
No vulnerabilities found.
No security vulnerabilities found.