Giter VIP home page Giter VIP logo

next-client-cookies's Introduction

Next.js Client Cookies

Client cookies with support for SSR on the newest Next.js 13 (app directory).

SSR are client components that can be rendered both on the client and server side. Server components are server only components that can only be rendered on the server side. This library can support all 3 cases and will give you everything you need to work with cookies on the client and server side.

Please note that Next.js currently does NOT support updating cookies on Server Components (only on Actions or Routes are allowed to make changes to cookies within the server). If you need to update cookies on the server, please switch to a Client Component. Client components can be rendered on the server side (via SSR) and will support also updating cookies via special dehydration mechanism implemented by this library.

Interface and client side implementation based on the js-cookie package.

Install

  1. Install the package:
npm add next-client-cookies
  1. On your app/layout.tsx file, add the CookiesProvider:
import { CookiesProvider } from 'next-client-cookies/server';

export default function RootLayout({ children }) {
  return <CookiesProvider>{children}</CookiesProvider>;
}

Please note this will make the entire app to opted out of static generation. If you want to opt out only some pages, you can use the CookiesProvider on a per page basis and create a custom server-side layout.tsx file for those pages.

Usage

Within a client side component

This will work BOTH on the client and server side for SSR.

'use client';

import { useCookies } from 'next-client-cookies';

const MyComponent = () => {
  const cookies = useCookies();

  return (
    <div>
      <p>My cookie value: {cookies.get('my-cookie')}</p>

      <button onClick={() => cookies.set('my-cookie', 'my-value')}>
        Set cookie
      </button>
      {' | '}
      <button onClick={() => cookies.remove('my-cookie')}>Delete cookie</button>
    </div>
  );
};

Within a server only component

Will produce the same Cookies interfaces using Next.js's cookies() helper.

import { getCookies } from 'next-client-cookies/server';

const MyComponent = () => {
  const cookies = getCookies();

  return (
    <div>
      <p>My cookie value: {cookies.get('my-cookie')}</p>
    </div>
  );
};

License

MIT

next-client-cookies's People

Contributors

deadlynjdj avatar moshest avatar stianjensen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.