
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
pinia-persistence-plugin
Advanced tools
Handle Pinia state persistence easily.
Install with your favorite package manager:
# npm
npm i pinia-persistence-plugin
# yarn
yarn add pinia-persistence-plugin
# pnpm
pnpm add pinia-persistence-plugin
Add this plugin to your Pinia instance:
import { createPinia } from 'pinia'
import { persistencePlugin } from 'pinia-persistence-plugin'
const pinia = createPinia()
pinia.use(persistencePlugin())
The persistence is enabled by default for all pinia stores and the default storage is localStorage
.
persistencePlugin({
// plugin options goes here
storeKeysPrefix: 'test',
persistenceDefault: false,
storageItemsDefault: [
{
storage: sessionStorage,
},
],
debug: true,
})
storeKeysPrefix?: string
: Defaults to undefined
. Add prefix to stores keys.
persistenceDefault?: boolean
: Defaults to true
. Whether to persist all stores by default.
storageItemsDefault?: PluginStorageItem[]
: Defaults to localStorage
. List of storages.
assertStorage?: (storage: Storage) => void | Promise<void>
: Perform a write and delete operation by default. To ensure storages
is available.
ensureAsyncStorageUpdateOrder?: boolean
: Ensure that the update of asynchronous storages is done in order
debug?: boolean
: Defaults to false
. Display errors and warnings in console.
storage?: Storage
: Where to store persisted state
includePaths?: string[]
: Defaults to []
. An array of any paths to partially persist the state
excludePaths?: string[]
: Defaults to []
. An array of any paths to exclude
serializer?: Serializer
: Object containing serialize and deserialize methods
serialize?: (state: S): any
: Defaults to JSON.stringify
. This method will be called right before storage.setItem
.
deserialize?: (value: any): any
: Defaults to JSON.parse
. This method will be called right after storage.getItem
.
defineStore(
'store',
() => {
const count = ref(0)
const increment = () => count.value++
return {
count,
increment,
}
},
{
persistence: {
// store options goes here
enabled: true,
storageItems: [
{
key: 'sample',
storage: sessionStorage,
includePaths: ['count'],
},
],
},
}
)
enabled?: boolean
: Enable persistence for the storestorageItems?: StorageItem[]
: List of storages.beforeHydrate?: (oldState: S) => void
: Perform some tasks before patching the storekey?: string
: Defaults to store.$id
. The persisted store state key.
And the same properties as PluginStorageItemgetItem: (key: string) => any | Promise<any>
setItem: (key: string, value: any) => void | Promise<void>
removeItem: (key: string) => void | Promise<void>
import { defineStore } from 'pinia'
import Cookies from 'js-cookie'
defineStore('example', {
// ...
persistence: {
enabled: true,
storageItems: [
{
storage: {
getItem: (key: string) => {
return Cookies.get(key)
},
setItem: (key: string, value: any) => {
Cookies.set(key, value)
},
removeItem: (key: string) => {
Cookies.remove(key)
},
},
},
],
},
})
import { defineStore } from 'pinia'
import localforage from 'localforage'
localforage.config({
driver: [localforage.INDEXEDDB],
name: 'Test IndexedDB',
storeName: 'store',
})
defineStore('example', {
// ...
persistence: {
enabled: true,
storageItems: [
{
storage: {
getItem: async (key: string) => {
return await localforage.getItem(key)
},
setItem: async (key: string, value: any) => {
await localforage.setItem(key, value)
},
removeItem: async (key: string) => {
await localforage.removeItem(key)
},
},
},
],
},
})
import { defineStore } from 'pinia'
import stringify from 'json-stringify-safe'
defineStore('example', {
// ...
persistence: {
enabled: true,
storageItems: [
{
storage: localStorage,
serializer: {
serialize: (value: any) => stringify(value),
},
},
],
},
})
import { defineStore } from 'pinia'
import SecureLS from 'secure-ls'
var ls = new SecureLS({
encodingType: 'aes',
isCompression: false,
})
defineStore('example', {
// ...
persistence: {
enabled: true,
storageItems: [
{
key: 'encrypted_data',
storage: {
getItem: (key: string) => {
try {
return ls.get(key)
} catch (error) {
console.error(error)
}
},
setItem: (key: string, value: any) => {
try {
ls.set(key, value)
} catch (error) {
console.error(error)
}
},
removeItem: (key: string) => {
try {
ls.remove(key)
} catch (error) {
console.error(error)
}
},
},
},
],
},
})
You can see an example of a full implementation here
here
This project is inspired by vuex-persistedstate and pinia-plugin-persist.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Detailed changes for each release are documented in the release notes.
This project is under MIT license.
FAQs
Pinia persistence plugin
The npm package pinia-persistence-plugin receives a total of 64 weekly downloads. As such, pinia-persistence-plugin popularity was classified as not popular.
We found that pinia-persistence-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.