-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: throw when a store is used outside of a Nuxt-aware context. #2857
base: v2
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for pinia-playground canceled.
|
✅ Deploy Preview for pinia-official canceled.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Do you have a reproduction of the problem where correctly using the useStore()
composable returned by defineStore()
doesn't work?
"correctly" no. The purpose of this PR is to ease the detection of incorrect usage. In the current version bad usage of a store after a await will work without warning in low-traffic environments. But, in cases of simultaneous requests, there is a possibility that pinia uses the pinia instance from another request. I can provide a reproduction if needed, but this is already a documented case:
For context: We recently found out that we had cross-request pollution caused by bad use of Pinia in Nuxt. With our large codebase, it was tough to track down every instance of the issue. So, we ended up creating a custom version of (I'll update the PR description with those infos.) |
Thanks for the great description of the problem! I agree it's a nice addition |
commit: |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v2 #2857 +/- ##
==========================================
- Coverage 88.75% 87.96% -0.79%
==========================================
Files 19 19
Lines 1449 1462 +13
Branches 226 226
==========================================
Hits 1286 1286
- Misses 162 175 +13
Partials 1 1 ☔ View full report in Codecov by Sentry. |
Nice! I'll complete the typing and refine the test to move this PR out of draft. |
Prefer the Nuxt Pinia instance over the global active Pinia instance. Since the Nuxt Pinia instance is discarded after each request, it ensures that we can't accidentally use one from another request. Additionally, `usePinia` will throw an error when used outside of a Nuxt-aware context. The error is as follows in dev : > [nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function.
2a0c9ed
to
d357d58
Compare
Context
The Pinia documentation states the following:
We recently discovered that we had cross-request pollution caused by bad use of Pinia stores after an await in Nuxt. With a large codebase, it was tough to track down every instance of the issue. So, we ended up creating a custom version of
defineStore
, similar to the one in this PR, to help prevent developers from misusing Pinia stores.I think it would be useful if this could be implemented directly in the Pinia Nuxt module.
Description of the changes
The Nuxt Pinia module does the following for each request:
The module also provides a helper
usePinia()
that retrieves the Pinia instance from the Nuxt context and not from the Pinia global variable. Conveniently, this helper will throw an error if used after anawait
(outside of a Nuxt-aware context)!This PR ensures that on the server we always use
usePinia()
to get the current Pinia instance. By doing souseStore
will systematically throw if used after an await on the server (similarly to theuseNuxtApp
composable)Breaking change
Server-side, using a store outside a Nuxt-aware context will now result in an error. Previously, it would continue without error, but sometimes, the wrong Pinia instance was used.
Error in dev:
Error in prod: