// this will blow up in nuxt SSR because it is not in a component.
const token = useCookie<string>('userToken', rawEncode)
const cookiesStorage = {
setItem (_key: string, state: string) {
try {
token.value = JSON.parse(state).userToken
} catch (err) {
return undefined
}
},
getItem (_key: string) {
try {
return JSON.stringify({ userToken: token.value })
} catch (err) {
return undefined
}
}
}
const useStore = defineStore({
// ...
persist: {
enabled: true,
strategies: [
{
storage: cookiesStorage,
paths: ['accessToken']
},
],
}
}
It runs before the useStore.
But this is problematic when using with SSR frameworks.
Because SSR frameworks (such as nuxt) relies on useSSRContext to retrieve of information about current request.
Which isn't available outside of component life cycle.
My current hack about this is
const useStore = defineStore({
// ...
strategies: [
{
get storage () { return getCookiesStorage() as unknown as Storage },
paths: ['userToken']
}
]
// ...
})
To delay the deference of getCookiesStorage and prevent it from crash the server.
But that isn't a proper usage at all.
I think this should be changed to (or as an alternative option) a factory function that called during useStore, so it don't cause probelm.
The custom storage is a Object parameter of pinia-plugin-persist
It runs
before
theuseStore
.But this is problematic when using with SSR frameworks.
Because SSR frameworks (such as nuxt) relies on
useSSRContext
to retrieve of information about current request. Which isn't available outside of component life cycle.My current hack about this is
To delay the deference of getCookiesStorage and prevent it from crash the server. But that isn't a proper usage at all.
I think this should be changed to (or as an alternative option) a factory function that called during useStore, so it don't cause probelm.