npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-persist

v1.2.4

Published

Client-side persistence with server-side rendering

Downloads

1,517

Readme


Getting Started

To add <NextPersistWrapper />, getLocalStore, and getCookieStore 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/lib/NextPersistWrapper';
  3. If utilizing localStorage to persist client-state: Import { getLocalStore } into your reducer(s) you plan to persist.

    // yourReducer.js
    import { getLocalStore } from 'next-persist'
  4. If utilizing cookies to persist client-state:

    Import { getCookieProps } into your frontend at the top level of your Next.js app as well as importing { getCookieStore } into any reducer(s) you plan to persist.

    // _app.js
    import { getCookieProps } from 'next-persist'
    
    // yourReducer.js
    import { getCookieStore } from 'next-persist'

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/lib/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 getLocalStore or getCookieStore from 'next-persist'.

Declare a constant and assign it the value of the evaluated result of calling getLocalStore or getCookieStore method.

getLocalStore or getCookieStore 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 { getLocalStore } from 'next-persist';
  // or
  // import { getCookieStore } from 'next-persist'

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

  const persistedState = getLocalStore('reducerOne', initialState);
  // or
  // const persistedState = getCookieStore('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 getCookieProps 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 = getCookieProps(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: