Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-persist

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-persist

Bridging the gap between client-side persistence and server-side rendering

  • 1.1.3-alpha.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
395
decreased by-58.98%
Maintainers
1
Weekly downloads
 
Created
Source

logo
GitHub Repo stars npm GitHub contributors NPM

next-persist

Bridging the gap between client-side persistence and server-side rendering.
Homepage →
Demo →

'What is next-persist?' Well, next-persist is a lightweight NPM package developed to simplify the process of storing and reconciling non-critical persistent client state while retaining the benefits of server side rendering and static site generation provided by Next.js.

Wouldn't it be nice to gain the benefits of Next.js while still providing the users with some sort of dynamic state persistence? How about without worrying about the design and added costs of additional database management systems?

Well now you can! next-persist provides a simple solution for your dynamic, isomorphic web applications. Just import next-persist, set up a quick config and incorporate our functions. We do the rest, delivering you the benefits of server side rendering and persistent client data.

screenshot of next-persist at _app level
Table of Contents
  1. Getting Started
  2. Usage
  3. Contributing
  4. License
  5. Maintainers
  6. Built with:

Getting Started

To add <NextPersistWrapper />, getStorage, and getCookie to your project, follow these steps.


Prerequisites

  • Redux (v. 4.0.5 and up)

    npm install redux
    
  • React (v. 16.8.0 and up)

    npm install react
    

Installation

  1. Install next-persist from the terminal.

    npm install next-persist
    
  2. Import <NextPersistWrapper /> into your frontend at top level of your Next.js app.

    // _app.js
    import PersistWrapper from 'next-persist/src/NextPersistWrapper';
    
  3. If utilizing localStorage to persist client-state:
    Import { getStorage } into your reducer(s) you plan to persist.

    // yourReducer.js
    import { getStorage } from 'next-persist'
    
  4. If utilizing cookies to persist client-state:
    Import { getCookie } into your frontend at the top level of your Next.js app as well as into any reducer(s) you plan to persist.

    // _app.js
    import { getCookie } from 'next-persist/src/next-persist-cookies'
    
    // yourReducer.js
    import { getCookie } from 'next-persist/src/next-persist-cookies'
    

Usage

Config

next-persist requires a simple config object allowing you to make changes to the behaviour of our package. First, a required method key, dictating which storage method you would like to use. Second, an optional allowList key holding an object.

  //_app.js

  const npConfig = {
    method: 'localStorage' or 'cookies'
    allowList: {
      reducerOne: ['stateItemOne', 'stateItemTwo'],
      reducerTwo: [],
    },
  };

The allowList key can be setup to allow only certain reducers to store only certain pieces of state to the chosen storage method. The keys on allowList have to correspond with the keys of the reducers in combineReducers(). To store only certain pieces of state from a reducer, set the value as an array holding the names of the state items as strings. If you wish to store all state from a reducer, set the value as an empty array. If no allowList is provided, next-persist will store all state from all reducers to the chosen storage method.


Wrapper

<PersistWrapper /> requires one prop with the label: wrapperConfig, which takes as argument config object that the developer declares in the _app component.

  Example:

  import { Provider } from "react-redux";
  import store from "../client/store";
  import PersistWrapper from 'next-persist/src/NextPersistWrapper';

  const npConfig = {
    method: 'localStorage'
    allowList: {
      reducerOne: ['stateItemOne', 'stateItemTwo'],
    },
  };

  const MyApp = ({ Component, pageProps }) => {
    return (
      <Provider store={store}>
        <PersistWrapper wrapperConfig={npConfig}>
          <Component {...pageProps} />
        </PersistWrapper>
      </Provider>
    );
  };

  export default MyApp;

Reducer

In each reducer file we need to import getStorage from 'next-persist' or or getCookies from 'next-persist/src/next-persist-cookies'.

Declare a constant and assign it the value of the evaluated result of calling getStorage or getCookies method.

getStorage or getCookies takes two arguments:

  • a string: the reducer key that is saved in storage
  • an object: the initial state declared in the reducer file

Pass in the newly declared constant into the reducer as a default parameter for state.

  Example:

  import * as types from '../constants/actionTypes';
  import { getStorage } from 'next-persist';
  // or
  // import { getCookies } from 'next-persist/src/next-persist-cookies'

  const initialState = {
    // initialState goes here
    stateItemOne: true,
    stateItemTwo: 0,
    stateItemThree: 'foo',
  };

  const persistedState = getStorage('reducerOne', initialState);
  // or
  // const persistedState = getCookies('reducerOne', initialState);


  const firstReducer = (state = persistedState, action) => {
    // switch case logic in here
    switch (action.type) {
    default:
      return state;
    }
  };

  export default firstReducer;

Cookies

Utilizing the cookie storage method offers the benefit of utilizing client state with getInitialProps. However it cannot be used to store large amounts of data due to the limits on cookie size.

In this example we invoke getCookie in getInitialProps and it will return back an object holding all the persisted state values, saved under the key of their reducer name.

  Example:

  MyApp.getInitialProps = async (ctx) => {
    const cookieState = getCookie(ctx);
    return {
      pageProps: cookieState,
    };
  }

  export default MyApp;

WARNING - NEVER STORE UNENCRYPTED PERSONAL DATA TO CLIENT STORAGE


Contributing

If you would like to contribute to next-persist, please fork this repo. Commit your changes to a well-named feature branch then open a pull request. We appreciate your contributions to this open-source project!

License

Distributed under the MIT License. See LICENSE for more information.

Maintainers

Built with:

Keywords

FAQs

Package last updated on 17 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc