Skip to main content

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'));
ArgumentTypeDescription
keystringCookie name.
valuestringCookie value.
timenumberLifetime in milliseconds, added to Date.now(). Use the setOneDay/setOneYear/… presets below.
pathstringURL path that must match for the cookie to be sent. Omit/falsy to skip.
domainstringDomain the cookie is scoped to. Omit/falsy to skip.
securebooleanOnly 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)
ArgumentTypeDescription
keystringCookie 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.

PresetValue (ms)
cookies.setHalfAnHour()1_800_00030 minutes
cookies.setHalfDay()43_200_00012 hours
cookies.setOneDay()86_400_00024 hours
cookies.setOneMonth()2_628_000_000~30.4 days
cookies.setOneYear()31_540_000_000~365 days
cookies.setCookie('preview', 'on', cookies.setHalfAnHour(), '/');