question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuxt instance disappears after calling $fetch in a composable (nuxt instance unavailable)

See original GitHub issue

Environment


  • 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Aug 10, 2022

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 and defineNuxtPlugin 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.

0reactions
danielroecommented, Aug 30, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found