prazdevs / pinia-plugin-persistedstate

💾 Configurable persistence and rehydration of Pinia stores.
https://prazdevs.github.io/pinia-plugin-persistedstate/
MIT License
2.13k stars 121 forks source link

A composable that requires access to the Nuxt instance was called outside of a plugin #211

Closed mudoo closed 1 year ago

mudoo commented 1 year ago

Describe the bug

Error: [nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at https://nuxt.com/docs/guide/concepts/auto-imports#using-vue-and-nuxt-composables.
    at Module.useNuxtApp (E:\web\feature\node_modules\nuxt\dist\app\nuxt.js:225:13)
    at Module.useRequestEvent (E:\web\feature\node_modules\nuxt\dist\app\composables\ssr.js:16:58)
    at readRawCookies (E:\web\feature\node_modules\nuxt\dist\app\composables\cookie.js:53:62)
    at Module.useCookie (E:\web\feature\node_modules\nuxt\dist\app\composables\cookie.js:23:19)
    at Object.setItem (E:\web\feature\node_modules\@pinia-plugin-persistedstate\nuxt\dist\runtime\storages.mjs:13:27)
    at persistState (file:///E:/News/web/feature/node_modules/pinia-plugin-persistedstate/dist/index.mjs:51:13)
    at store.$subscribe.detached (file:///E:/News/web/feature/node_modules/pinia-plugin-persistedstate/dist/index.mjs:113:11)
    at file:///E:/News/web/feature/node_modules/pinia/dist/pinia.mjs:1406:21
    at callWithErrorHandling (E:\web\feature\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:156:18)
    at callWithAsyncErrorHandling (E:\web\feature\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:164:17)

Reproduction

persist: [{ debug: true }]

System Info

window10
node 16.20.0
nuxt 3.5.3

Used Package Manager

yarn

Validations

mudoo commented 1 year ago

server side is not working.

mudoo commented 1 year ago

is nuxt3 context bug, not this plugin problem.

zllvm commented 1 year ago

It does it seem like it is a nuxt3 bug, but intended behavior. Could it be fixed by wrapping useCookie with callWithNuxt in this module?

dasph commented 2 months ago

I've managed to solve this problem by manually defining a nuxt plugin where I would call $pinia.use(createPersistedState(...)) (instead of adding pinia-plugin-persistedstate/nuxt to modules inside of nuxt.config.ts

In my case the error was thrown because of runWithContext(...) that's called when you add this plugin trough nuxt.config.

prazdevs commented 2 months ago

i will try to use the callWithNuxt thing and see what happens