Use universal-cookie for JavaScript
mainuniversal-cookie for general-purpose cookie management in any JavaScript environment. It serves as the core library for handling cookies across different platforms.repository·main·Indexed 42 days ago
https://github.com/itsbencodes/cookiesA collection of libraries for universal cookie management across JavaScript environments. Includes universal-cookie for general-purpose use, react-cookie (v8.1.2) for React applications via hooks and HOCs, and middleware for Express (universal-cookie-express) and Koa (universal-cookie-koa) to support server-side rendering (SSR).
universal-cookie for general-purpose cookie management in any JavaScript environment. It serves as the core library for handling cookies across different platforms.universal-cookie-koa to enable cookie getting and setting on Koa servers. This is specifically designed to support server-side rendering (SSR) workflows where cookies need to be accessible during the request lifecycle on Koa.universal-cookie-express to enable cookie getting and setting on Express servers. This is specifically designed to support server-side rendering (SSR) workflows where cookies need to be accessible during the request lifecycle on Express.react-cookie when you need to manage cookies within a React application. It provides universal cookie support specifically tailored for React components and hooks.To use universal-cookie-koa in your Koa project, install it via npm:
npm install universal-cookie-koa --saveTo use react-cookie in your project, install it via npm:
npm install react-cookieAlternatively, you can include it in the browser using global variables via unpkg scripts (requires react and universal-cookie to be loaded first).
To run the demo project, which showcases the usage of react-cookie and universal-cookie-express, you must first prepare the monorepo environment from the root directory before starting the demo server.
From the root directory of the repository, run:
lerna bootstrap to link dependencies.npm run build to build the necessary packages.Navigate to the demo directory and run:
npm start to launch the demo server with server-rendering enabled.# In root directory
lerna bootstrap
npm run build
# In demo directory
npm startTo support SSR (e.g., with Express), you must bridge the server's request cookies to the React application using universal-cookie-express and the CookiesProvider's cookies prop.
universal-cookie-express middleware in your Express app to populate req.universalCookies.req.universalCookies to <CookiesProvider cookies={req.universalCookies}>.<CookiesProvider> normally.// server.ts
import express from 'express';
import serverMiddleware from './src/server';
import cookiesMiddleware from 'universal-cookie-express';
const app = express();
// Use the middleware to populate req.universalCookies
app.use(cookiesMiddleware()).use(serverMiddleware);
app.listen(8080);
// src/server.ts
// Inside your middleware function:
const markup = ReactDOMServer.renderToString(
<CookiesProvider cookies={req.universalCookies}>
<App />
</CookiesProvider>
);To use universal-cookie-express in your Express application, install it via npm:
npm install universal-cookie-express --saveYou can install universal-cookie via npm:
npm install universal-cookieAlternatively, you can use it in the browser via a <script> tag using the UMD build:
<script
crossorigin
src="https://unpkg.com/universal-cookie@7/umd/universalCookie.min.js"
></script>When using the script tag, the library is available via the global variable UniversalCookie.
npm install universal-cookieUse set() to create or update a cookie and remove() to delete one.
set(name, value, [options])
name (string): Cookie name.value (string|object): The value to store. If an object is provided, it will be stringified.options (object): Supports all RFC 6265 options (path, expires, maxAge, domain, secure, httpOnly, sameSite, partitioned).remove(name, [options])
name (string): Cookie name.options (object): Supports all RFC 6265 options. Note that the path and domain must match the original cookie to successfully remove it.import Cookies from 'universal-cookie';
const cookies = new Cookies({ path: '/' });
// Set a cookie
cookies.set('myCat', 'Pacman');
// Set an object
cookies.set('user', { id: 1, name: 'Ben' });
// Remove a cookie
cookies.remove('myCat');Use get() to retrieve a specific cookie by name, or getAll() to retrieve all available cookies.
get(name, [options])
name (string): The name of the cookie.options.doNotParse (boolean): If true, the value is returned as a raw string without attempting to parse it as an object.getAll([options])
options.doNotParse (boolean): If true, cookies are returned as raw strings without object parsing.import Cookies from 'universal-cookie';
const cookies = new Cookies();
const value = cookies.get('myCat');
const allCookies = cookies.getAll();