Deep Dive Into Nuxt 3 Server Side Data Fetching
31 August 2023
Table of contents
The Error
- During my exploration of Nuxt 3's data fetching capabilities, I discovered the useAsyncData composable, which is designed to simplify fetching data asynchronously on the server-side.
- However, when I attempted to use multiple composables within the asyncData hook, I encountered the following error: "A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function."
- However, when I attempted to use multiple composables within the asyncData hook, I encountered the following error: "A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function."
// composables/useArticle.js
return new Promise(async (resolve) => {
// useArticleById and useFeaturedArticleId both needs useDirectusItems() composable methods.
// Error occurs
const featureArticleId = await useFeaturedArticleId(); // first composable
const featuredArticle = await useArticleById( // second composable. Nuxt instance is lost here
featureArticleId,
getItemById
);
resolve(featuredArticle);
});
export const useArticleById = (id, getItemById) => {
// In this method, Nuxt instance is lost and causes Error.
// Cannot call composable anymore.
const { getItemById } = useDirectusItems();
return getItemById({
collection: "xyz_articles",
id,
params: {
fields,
},
});
};
export const useFeaturedArticleId = () => {
return useArticleSettings().then((settings) => {
return settings.featured_article;
});
};
Understanding the Error
- It's important to grasp the context in which Nuxt 3 data fetching functions operate.
- Nuxt.js utilizes server middleware and plugins to set up the application's environment before rendering the components.
- The useAsyncData composable, along with other composables, relies on this setup to function properly.
- Consequently, when attempting to call multiple composables directly within the useAsyncData hook, the error arises because the necessary Nuxt instance context is missing.
The Solution
- I devised a solution that involved rearranging the way I called the composables within the useAsyncData hook.
- By calling the neccessary composables at start of method and passing it in respective composables, I was able to ensure that the required composables had access to the Nuxt instance.
- Here's an example of how I implemented the solution
// composables/useArticle.js
return new Promise(async (resolve) => {
const { getItemById } = useDirectusItems(); // Using the neccessary composable at start
const featureArticleId = await useFeaturedArticleId(); // first composable
const featuredArticle = await useArticleById( // second composable. Nuxt instance is lost here
featureArticleId,
getItemById
);
resolve(featuredArticle);
});
// ...other composables same except remove the getItemById since it is available at start of composable
Important Information
- ❌ Don't use asyncData hook. As mentioned in documentation, Recommended method is
Conclusion
- Working with Nuxt 3 and the useAsyncData composable can provide significant advantages for server-side data fetching.
- However, it's important to be aware of the error that can occur when calling multiple composables directly within the asyncData hook.
- By understanding the underlying cause of the error and implementing a solution developers can avoid this issue and leverage the power of Nuxt 3's data fetching capabilities.
- By sharing my experience and providing a solution to the error I encountered, I hope to assist fellow developers in navigating similar challenges and enable them to fully leverage the capabilities of Nuxt 3.
Helpful Links
WRITTEN BY

Bhavin Panchal
Bhavin is a Nuxt.js maestro, building fast, scalable, and optimized web applications. He turns technical challenges into smooth user experiences, making sure that every app he touches performs at its best.
WRITTEN BY
Bhavin Panchal
Bhavin is a Nuxt.js maestro, building fast, scalable, and optimized web applications. He turns technical challenges into smooth user experiences, making sure that every app he touches performs at its best.
More
An interesting read? Here is more related to it.
12 August 2024
Pruthvi MenparaMaking IT Possible
Making IT Possible
Making IT Possible
Making IT Possible
Making IT Possible
Making IT Possible
India (HQ)
201, iSquare Corporate Park, Science City Road, Ahmedabad-380060, Gujarat, India
For Sales
[email protected]
Looking For Jobs
Apply Now