ランキングに戻る

maticzav/nookies

TypeScript

🍪 A set of cookie helpers for Next.js

cookienextjsreactzeithacktoberfest
スター成長
スター
2.4k
フォーク
76
週間成長
Issue
16
1k2k
2018年1月2020年11月2023年9月2026年7月
成果物npmnpm install nookies
README

nookies :cookie:

Working npm version

A collection of cookie helpers for Next.js

Features

  • ✨ SSR support, for setter, parser and destroy
  • ⚙️ Custom Express server support
  • 🪶 super light
  • 🛡 perfect for authentication

Setting and destroying cookies also works on server-side.

Quick start

yarn add nookies

You can play with the example code here.

ServerSide cookies

import nookies from 'nookies'

export default function Me() {
  return <div>My profile</div>
}

export async function getServerSideProps(ctx) {
  // Parse
  const cookies = nookies.get(ctx)

  // Set
  nookies.set(ctx, 'fromGetInitialProps', 'value', {
    maxAge: 30 * 24 * 60 * 60,
    path: '/',
  })

  // Destroy
  // nookies.destroy(ctx, 'cookieName')

  return { cookies }
}

Client-only Cookies

import { parseCookies, setCookie, destroyCookie } from 'nookies'

function handleClick() {
  // Simply omit context parameter.
  // Parse
  const cookies = parseCookies()
  console.log({ cookies })

  // Set
  setCookie(null, 'fromClient', 'value', {
    maxAge: 30 * 24 * 60 * 60,
    path: '/',
  })

  // Destroy
  // destroyCookie(null, 'cookieName')
}

export default function Me() {
  return <button onClick={handleClick}>Set Cookie</button>
}

Custom Express server cookies

const express = require('express');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const { parseCookies, setCookie, destroyCookie } = require('nookies');

app.prepare()
    .then(() => {
        const server = express();

        server.get('/page', (req, res) => {

          // Notice how the request object is passed
          const parsedCookies = parseCookies({ req });

          // Notice how the response object is passed
          setCookie({ res }, 'fromServer', 'value', {
            maxAge: 30 * 24 * 60 * 60,
            path: '/page',
          });

          // destroyCookie({ res }, 'fromServer');

          return handle(req, res);
        });

    );

Reference

For client side usage, omit the ctx parameter. You can do so by setting it to an empty object ({}), null or undefined.

parseCookies(ctx, options) or nookies.get(ctx, options)

  • ctx: Next.js context || (Express request object)
  • options:
    • decode: a custom resolver function (default: decodeURIComponent)

setCookie(ctx, name, value, options) or nookies.set(ctx, name, value, options)

Don't forget to end your response on the server with res.send().

  • ctx: (Next.js context) || (Express request object)
  • name: cookie name
  • value: cookie value
  • options:
    • domain
    • encode
    • expires
    • httpOnly
    • maxAge
    • path
    • sameSite
    • secure

destroyCookie(ctx, name, options) or nookies.destroy(ctx, 'token', options)

Don't forget to end your response on the server with res.send(). This might be the reason your cookie isn't removed.

  • ctx: (Next.js context) || (Express response object)
  • name: cookie name
  • options:
    • domain
    • path

License

MIT

関連リポジトリ
js-cookie/js-cookie

A simple, lightweight JavaScript API for handling cookies, client-side.

JavaScriptnpmMIT Licensecookiejavascript
22.6k2k
Justson/AgentWeb

AgentWeb is a powerful library based on Android WebView.

JavaMavenApache License 2.0webviewagentweb-android-webview
jianshu.com/p/fc7909e24178
9.4k1.7k
lining0806/PythonSpiderNotes

Python入门网络爬虫之精华版

PythonPyPIpythonzhihu
7.5k2.2k
jonasstrehle/supercookie

⚠️ Browser fingerprinting via favicon!

HTMLMIT Licensefingerprintfavicon
supercookie.me
7.4k367
nICEnnnnnnnLee/BilibiliDown

(GUI-多平台支持) B站 哔哩哔哩 视频下载器。支持稍后再看、收藏夹、UP主视频批量下载|Bilibili Video Downloader 😳

JavaMavencookiebilibili
nicennnnnnnlee.github.io/BilibiliDown/
5.1k444
github/secure_headers

Manages application of security headers with many safe defaults

RubyRubyGemsMIT Licensesecure-headerscsp
3.2k251
gorilla/sessions

Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.

GoGo ModulesBSD 3-Clause "New" or "Revised" Licensegorillacookie
gorilla.github.io
3.1k378
easychen/CookieCloud

CookieCloud是一个和自架服务器同步浏览器Cookie和LocalStorage的小工具,支持端对端加密,可设定同步时间间隔。本仓库包含了插件和服务器端源码。CookieCloud is a small tool for synchronizing browser cookies and LocalStorage with a self-hosted server. It supports end-to-end encryption and allows for setting the synchronization interval. This repository contains both the plugin and the server-side source code

TypeScriptnpmGNU General Public License v3.0cookiecookies
cc.ft07.com
3.1k278
grevory/angular-local-storage

An AngularJS module that gives you access to the browsers local storage with cookie fallback

JavaScriptnpmOtherangularangularjs
2.8k571
franciscop/brownies

🍫 Tastier cookies, local, session, and db storage in a tiny package. Includes subscribe() events for changes.

TypeScriptnpmcookielocalstorage
2.5k59
expressjs/cookie-parser

Parse HTTP request cookies

JavaScriptnpmMIT Licensejavascriptexpressjs
2k230
Cookie-AutoDelete/Cookie-AutoDelete

Firefox and Chrome WebExtension that deletes cookies and other browsing site data as soon as the tab closes, domain changes, browser restarts, or a combination of those events.

TypeScriptnpmMIT Licensewebextensionfirefox
2k135