This library, as the name suggests, allows you to easily interact with cookies in the browser. No longer do you need to parse some complicated URI encoded string, instead, we've collected them already for you. Below we'll quickly go over how you can use this library.
const id = Cookie.trackingID;
Cookie.trackingID = id;
delete Cookie.trackingID;
"trackingID" in Cookie;
for (const name in Cookie) {
console.log(name);
}
There you have it, that's most there is to this library. If you need some more customization options, do keep reading.
It is also possible to customize the options of cookies, but we have provided sane defaults so you will rarely need this.
Cookie.trackingID = new Cookie(id, {
path: "/subfolder/",
});
The the following options are available:
Key | Type | Default | |
---|---|---|---|
path |
string |
'/' |
The path for this cookie. |
domain |
string |
location.hostname |
The domain to which this cookie belongs, check MDN for some implementation notes. |
secure |
boolean |
true if the page is served over https |
Set to true if this cookie should only be available in secure contexts. |
sameSite |
'lax' ,'strict' or 'none' |
'lax' |
The same site behaviour of this cookie. |
maxAge |
number |
14 * 24 * 60 * 60 , 14 days |
The maximum age in seconds before this cookie should be removed, mutually exclusive with the expires parameter. |
expires |
Date |
The date this cookie should expire, mutually exclusive with the maxAge parameter. |