-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Nuxt instance disappears after calling $fetch in a composable (nuxt instance unavailable) #14526
Comments
Basically after an async operation, we no longer have access to the nuxt context (to avoid sharing state with other requests). Vue composables should almost always be synchronous, with a very few exceptions. In general you should get a reference NuxtApp at the beginning of your composable, and then consume/use it afterwards, rather than getting it at the point of use. Nuxt does perform a transform for I think this probably needs to be explained more clearly in the docs. |
Thanks for the explanation, I generally only made fetch related composables async. I agree having some alert regarding this is a good idea. |
If I need to use multiple composables to fetch few things through a pinia store (fetch a user then notifications for example), how would I go around that? Should I reference nuxt context there? As far as I see if I just call all of them through the middleware, all works fine, but if I try to call the store function it will fail. In general I use a middleware to initialize the store and fetch stuff like user and whatnot. |
I found a solution: https://github.com/nuxt/framework/discussions/5655#discussioncomment-3047558 Perhaps stuff like this could be shown in the docs too? |
Let's track in #14723. |
Environment
Windows_NT
v16.11.1
3.0.0-rc.6-27668034.5232c1b
yarn@1.22.15
vite
vite
-
-
Reproduction
https://github.com/Luffyyy/nuxt-instance-bug
https://stackblitz.com/edit/github-eh8ppf?file=composables/useTest.js
Describe the bug
I'm not super sure how to describe the bug, but in my main project I've had weird situations where a fetch request would run just fine and then another one would error with "nuxt instance unavailable". But I got it to a point it does something that is obviously not normal and allowing me to call anymore composables that rely on Nuxt instance.
Essentially running any composable that needs nuxt instance (useNuxtApp), fetch and then useNuxtApp again errors with "nuxt instance unavailable" which doesn't make any sense.
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: