Nuxt instance disappears after calling $fetch in a composable (nuxt instance unavailable)
See original GitHub issueEnvironment
- Operating System:
Windows_NT
- Node Version:
v16.11.1
- Nuxt Version:
3.0.0-rc.6-27668034.5232c1b
- Package Manager:
yarn@1.22.15
- Builder:
vite
- User Config:
vite
- Runtime Modules:
-
- Build Modules:
-
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
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
nuxt.js - Server-side fetched data appears, disappears and re ...
For example: The logo is there due to being loaded on server side; Client side fetches the logo asset; Logo disappears; Client side...
Read more >Using Modules and Pinia to structure Nuxt 3 app
In this page we will use previously created composable, component, and Pinia store to display this content and value of current blog id...
Read more >Writing content - Nuxt Content
Learn how to write your content/, supporting Markdown, YAML, CSV and JSON.
Read more >Nuxt3 doesn't fetch new data after client-side navigation : r/Nuxt
I'm struggling with data fetching in Nuxt3. I want to have a page which lists blog posts and when you click on a...
Read more >Integrating e-commerce platform | Vue Storefront 2
One such example would be isInCart , which accepts currentCart and product as ... This function is called when composable is created and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
defineNuxtRouteMiddleware
anddefineNuxtPlugin
to allow using nuxt composables after async operations, and Vue does the same within<script setup>
. But those are the exceptions, not the rule.I think this probably needs to be explained more clearly in the docs.
Let’s track in https://github.com/nuxt/framework/issues/6996.