I am updating a Nuxt2 project to Nuxt3. Before I used Vuex to share some data across my app. I loaded the navigation structure from an endpoint and could use that information across my components.
Now in Nuxt 3 I thought I could write a composable useNavigation
which fetches the navigation and then inside my components I could use that data without needing to fetch over and over again.
Now I run into two problems:
- Using async/await in composables seems to be a problem. I have a working example, but I still get warnings, that a composable requires access to the Nuxt instance, which seems to be related with my async/await stuff inside my composable.
- I realize that when generating the pages, the page does not wait for the composable to have that data ready. If I instead use
useFetch
directly in my components/pages, these things are awaited and therefore appear in my generated pages. Things inside my composable are not awaited.
So my main question is: How should I handle data that is used across multiple pages which I don't want to refetch all the time? Should I write at least a mixin and use that to DRY
(don't repeat yourself)? Will Nuxt keep a solid cache for such situations so that this does not take too much time?
Or how could I fetch once, make sure that these things are available and then reuse it in my app?