Cookies
The cookies helper provides three small functions:
set(),
get()
and
remove(). They're thin wrappers around
document.cookie
with encoding
and sensible defaults.
Here's how you set a cookie:
import * as cookies from "/assets/vendor/qite/src/helpers/cookies.js";
cookies.set("theme", "dark");
Options for
set()
are:
-
path(default: "/") -
domain -
days(number; sets Max-Age and Expires) -
sameSite("Lax" | "Strict" | "None") -
secure(boolean)
Example setting a cookie with expiration:
cookies.set("remember_me", "1", { days: 30, sameSite: "Lax" });
If
days
is omitted, a session cookie is created. Names and values are
URI-encoded automatically.
To
read
an existing cookie, use
get(). It reads a cookie by name,
returns the decoded value or
null
if not found. If decoding fails,
returns the raw value:
cookies.get("theme");
To
delete
a cookie use
remove(). It deletes cookies by internally setting them
with
days: 0
cookies.remove("theme");
Path and domain must match the values used during
set()