# next-persist

> Client-side persistence with server-side rendering

Latest version **1.2.4** (published 2021-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install next-persist
pnpm add next-persist
yarn add next-persist
bun add next-persist
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2021-04-27 |
| First published | 2021-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 21.8 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 98 |
| Author | most.js |
| Maintainers | most-js |
| Keywords | next.js, react, redux, persist, localStorage, cookies, client-side rendering, server-side rendering |

## Links

- npm: https://www.npmjs.com/package/next-persist
- Repository: https://github.com/oslabs-beta/next-persist
- Homepage: https://github.com/oslabs-beta/next-persist#readme
- Issues: https://github.com/oslabs-beta/next-persist/issues
- npm.io page: https://npm.io/package/next-persist

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) ^0.4.1
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.4 (latest) — 2021-04-27
- 1.2.2-alpha.0 (alpha) — 2021-04-20
- 1.2.3 — 2021-04-27
- 1.2.2 — 2021-04-20
- 1.2.1 — 2021-04-20
- 1.2.1-alpha.8 — 2021-04-20
- 1.2.1-alpha.7 — 2021-04-20
- 1.2.1-alpha.6 — 2021-04-20
- 1.2.1-alpha.5 — 2021-04-20
- 1.2.1-alpha.4 — 2021-04-20
- 1.2.1-alpha.3 — 2021-04-20
- 1.2.1-alpha.2 — 2021-04-20
- 1.2.1-alpha.1 — 2021-04-20
- 1.2.1-alpha.0 — 2021-04-20
- 1.2.0 — 2021-04-19
- … 21 more at https://npm.io/package/next-persist/versions

## README

<p align="center">
  <img src="./assets/NEXT-PERSIST-A-trim.png" alt="logo" width="100%"/>
  <br />
  </a>
  <a href="https://github.com/oslabs-beta/next-persist/stargazers">
    <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/oslabs-beta/next-persist?color=%233ac0ba%0A&style=for-the-badge">
  </a>
  <a href="https://www.npmjs.com/package/next-persist">
    <img alt="npm" src="https://img.shields.io/npm/v/next-persist?color=%233ac0ba%0A&style=for-the-badge">
  </a>
  <a href="https://github.com/oslabs-beta/next-persist/graphs/contributors">
    <img alt="GitHub contributors" src="https://img.shields.io/github/contributors/oslabs-beta/next-persist?color=%233ac0ba%0A&style=for-the-badge">
  <a href="https://github.com/oslabs-beta/next-persist/blob/main/LICENSE">
    <img alt="NPM" src="https://img.shields.io/npm/l/next-persist?color=%233ac0ba%0A&style=for-the-badge">
  </a>
</p>

<h1 align="center">next-persist</h1>
<p align="center">
Bridging the gap between client-side persistence and server-side rendering.
  <br />
    <a href="https://www.mostjs.org">Homepage →</a><br>
    <a href="https://www.mostjs.org/demo">Demo →</a>
</p>

<table>
  <tr>
    <td width="60%">
<p>'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.</p>
<p>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?
</p>
<p>
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.
</p>
   </td>
    <td>
      <img src="./assets/screenshot1.png" alt="screenshot of next-persist at _app level" width="100%">
    </td>
  </tr>
</table>

<details open="open">
  <summary>Table of Contents</summary>
    <ol>
      <li>
        <a href="#getting-started">Getting Started</a>
        <ul>
          <li><a href="#prerequisites">Prerequisites</a></li>
          <li><a href="#installation">Installation</a></li>
        </ul>
      </li>
      <li>
        <a href="#usage">Usage</a>
        <ul>
          <li><a href="#config">Config</a></li>
          <li><a href="#wrapper">Wrapper</a></li>
          <li><a href="#reducer">Reducer</a></li>
          <li><a href="#cookies">Cookies</a></li>
        </ul>
      </li>
      <li><a href="#contributing">Contributing</a></li>
      <li><a href="#license">License</a></li>
      <li><a href="#maintainers">Maintainers</a></li>
      <li><a href="#built-with">Built with:</a></li>
    </ol>
</details>

---

## 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:<br>
    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:<br>

    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](https://github.com/oslabs-beta/next-persist). 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

- [Brian Chu](https://github.com/darthchu)
- [Christopher Bosserman](https://github.com/christopherpbosserman)
- [Greg Levine-Rozenvayn](https://github.com/grishaLR)
- [Matthew Salvador](https://github.com/mjsalvador)

## Built with:

- [Next.js](https://nextjs.org/)
- [React](https://reactjs.org/)
- [React-Redux](https://react-redux.js.org/)
- the support of [OSLabs](https://github.com/open-source-labs)

---
_Source: https://npm.io/package/next-persist · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
