cookies
A small object with helpers for reading and writing document.cookie, plus a few presets for common expirations.
import { cookies } from '@eightshift/frontend-libs-tailwind/scripts';
cookies.setCookie('gdpr', '2', cookies.setOneDay(), '/', '.example.com', true, 'Strict');
const consent = cookies.getCookie('gdpr');
setCookie
cookies.setCookie(key, value, time, path, domain, (secure = true), (sameSite = 'Lax'));
| Argument | Type | Description |
|---|---|---|
key | string | Cookie name. |
value | string | Cookie value. |
time | number | Lifetime in milliseconds, added to Date.now(). Use the setOneDay/setOneYear/… presets below. |
path | string | URL path that must match for the cookie to be sent. Omit/falsy to skip. |
domain | string | Domain the cookie is scoped to. Omit/falsy to skip. |
secure | boolean | Only send the cookie over HTTPS. Defaults to true. |
sameSite | 'Strict' | 'Lax' | 'None' | SameSite attribute. Defaults to 'Lax'. |
Returns true on success, false on failure (and logs the error).
getCookie
cookies.getCookie('gdpr');
// → '2' (or null if not set)
| Argument | Type | Description |
|---|---|---|
key | string | Cookie name. |
Returns the cookie value as a string, or null if the cookie isn't set.
Expiration presets
Each returns a number of milliseconds you can pass as the time argument to setCookie.
| Preset | Value (ms) | ≈ |
|---|---|---|
cookies.setHalfAnHour() | 1_800_000 | 30 minutes |
cookies.setHalfDay() | 43_200_000 | 12 hours |
cookies.setOneDay() | 86_400_000 | 24 hours |
cookies.setOneMonth() | 2_628_000_000 | ~30.4 days |
cookies.setOneYear() | 31_540_000_000 | ~365 days |
cookies.setCookie('preview', 'on', cookies.setHalfAnHour(), '/');