Modern Cookie Utility for Browsers

1.0.1 · active · verified Tue Apr 21

js-cookie-next is a native-first, TypeScript-first utility library designed for managing browser cookies with a focus on modern web standards. It transparently supports the Asynchronous Cookie Store API where available and falls back to `document.cookie` for broader compatibility. Currently at version 1.0.1, the library maintains a stable release cadence, with updates primarily driven by new browser features or bug fixes rather than frequent major API changes. Key differentiators include its zero-dependency footprint, small size (< 2 KB gzipped), SSR-safe imports that make sync APIs no-ops in non-browser environments, and built-in support for Partitioned Cookies (CHIPS) via a simple preset option. It prioritizes type safety and a clear API for both synchronous and asynchronous cookie operations, catering to contemporary web development practices.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the synchronous and asynchronous APIs, including setting and getting cookies, removing them, and utilizing advanced options like `partitioned` mode for CHIPS.

import { get, set, remove, getAsync, setAsync, removeAsync } from "js-cookie-next";

// Basic synchronous usage
set("theme", "dark", { path: "/", sameSite: "lax" });
console.log('Current theme (sync):', get("theme"));
remove("theme", { path: "/" });
console.log('Theme after removal (sync):', get("theme"));

// Asynchronous usage with fallback
async function manageAsyncCookie() {
  await setAsync("session_id", "user_xyz", { expires: 7, secure: true, sameSite: "none" });
  const sessionId = await getAsync("session_id");
  console.log('Session ID (async):', sessionId);

  // Example of a partitioned cookie (CHIPS)
  await setAsync("widget_data", "value123", { mode: "partitioned" });
  console.log('Widget data set with CHIPS (async).');

  await removeAsync("session_id");
  console.log('Session ID after removal (async):', await getAsync("session_id"));
}

manageAsyncCookie();

view raw JSON →