Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pinia-plugin-persistedstate

prazdevs1.1mMIT4.2.0TypeScript support: included

Configurable persistence and rehydration of Pinia stores.

vue, store, pinia, persistence, pinia-plugin, nuxt, nuxt-module

readme

Pinia Plugin Persistedstate

Configurable persistence and rehydration of Pinia stores.

npm minizipped size license

Documentation

Features

  • Persist Pinia stores with a friendly API inspired by vuex-persistedstate.
  • Highly customizable (storage, serializer, paths picking/omitting).
  • Out of the box SSR-friendly support for Nuxt.
  • Very smol (<2kB minzipped).

Quickstart

  1. Install with your favorite package manager:

    • pnpm : pnpm add pinia-plugin-persistedstate
    • npm : npm i pinia-plugin-persistedstate
    • yarn : yarn add pinia-plugin-persistedstate
  2. Add the plugin to pinia:

import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
  1. Add the persist option to the store you want to be persisted:
import { defineStore } from 'pinia'

export const useStore = defineStore('store', {
  state: () => ({
    someState: 'hello pinia',
  }),
  persist: true,
})

Configuration

You can configure how a store is persisted by specifying options to the persist property:

export const useStore = defineStore('store', () => {
  const someState = ref('hello pinia')
  return { someState }
}, {
  persist: {
    storage: sessionStorage,
    pick: ['someState'],
  },
})

All the available configuration options are explained here.

Usage with Nuxt

Nuxt support comes out of the box thanks to the included module. You just need to install the package and add the module to your nuxt.config.ts as follows:

export default defineNuxtConfig({
  modules: [
    '@pinia/nuxt', // required
    'pinia-plugin-persistedstate/nuxt',
  ],
})

More information on storages and configuration in Nuxt here.

Limitations

There are several limitations that should be considered, more on those here.

Contributing

See the contribution guide.

License

MIT © 2021-present Sacha Bouillez

changelog

Changelog

v4.2.0

compare changes

🚀 Enhancements

  • nuxt: Infer global cookieOptions to specified cookie storage without options (3c55848)
  • nuxt: Explicit Nuxt plugin name (pinia-plugin-persistedstate) (a0a4c51)

🩹 Fixes

  • nuxt: Check @pinia/nuxt module installation by pinia key (#359)

🏡 Chore

  • Bump dependencies (incl. pinia^2.3.0 & @pinia/nuxt^0.9.0) (c71d618)

❤️ Contributors

v4.1.3

compare changes

📖 Documentation

  • Fix the afterHydrate describe (#350)
  • Fix install script groups (2bd0f25)

📦 Build

  • Bundle iife and umd with rollup (fcad302)

🏡 Chore

❤️ Contributors

v4.1.2

compare changes

📖 Documentation

  • Add Chinese translation (#338)
  • Add cookie max size info (#343)

📦 Build

  • Export iife build (#349)

🏡 Chore

  • Bump dependencies & lint (3418005)

❤️ Contributors

v4.1.1

compare changes

🩹 Fixes

  • Bump deep-pick-omit to 1.2.1 (fixes falsy value persistence) (7be6b42)

❤️ Contributors

v4.1.0

compare changes

🚀 Enhancements

📖 Documentation

  • Update Nuxt links in README.md (#340)

🏡 Chore

❤️ Contributors

v4.0.2

compare changes

🩹 Fixes

  • Infer CookieStorageOptions from @nuxt/schema rather than nuxt/schema (9e77053)
  • nuxt: Avoid calling runWithContext (#334)

📖 Documentation

  • Improve readability (code group icons + code titles) (13b6332)

🏡 Chore

❤️ Contributors

v4.0.1

compare changes

🩹 Fixes

  • Augment @nuxt/schema rather than nuxt/schema (#330)

📖 Documentation

  • Add group icon plugin (#329)

❤️ Contributors

v4.0.0

🚀 Enhancements

  • Support excluding paths from persistence with omit option
  • Support autocompletion for dot-notation paths in pick and omit options
  • ⚠️ Rehydrate only picked/omitted paths (when specified) instead of the whole state
  • ⚠️ Drop global beforeRestore and afterRestore options
  • ⚠️ Drop auto mode
  • nuxt: Include Nuxt module in the base package
  • nuxt: Add global key option using template string

🔥 Performance

  • Deserialize with destr instead of JSON.parse

🩹 Fixes

  • nuxt: Improve SSR handling and store hdyration/persistence behavior in route middleware

💅 Refactors

  • ⚠️ Rename paths option to pick
  • ⚠️ Rename beforeRestore and afterRestore options to beforeHydrate and afterHydrate
  • nuxt: ⚠️ Rename auto-imported storage object to piniaPluginPersistedstate
  • nuxt: ⚠️ Rename Nuxt config option key to piniaPluginPersistedstate

📖 Documentation

  • Rewrite documentation to match v4 API
  • Add references and links to Nuxt-specific details and implementations

📦 Build

  • Build standalone from Nuxt module's runtime

🎨 Styles

  • Add a new identity and logo to the project

⚠️ Breaking Changes

  • ⚠️ Rehydrate only picked/omitted paths (when specified) instead of the whole state
  • ⚠️ Drop global beforeRestore and afterRestore options
  • ⚠️ Drop auto mode
  • ⚠️ Rename paths option to pick
  • ⚠️ Rename beforeRestore and afterRestore options to beforeHydrate and afterHydrate
  • nuxt: ⚠️ Rename auto-imported storage object to piniaPluginPersistedstate
  • nuxt: ⚠️ Rename Nuxt config option key to piniaPluginPersistedstate

❤️ Contributors


For changes prior to v4, please refer to the GitHub Releases