Where to fetch initial user data
What is the best practice to load initial user data? Currently I do it in the default.vue layout in an onMounted hook, but that doesn't seem like the right way as sometimes it doesn't work.
Here is the code:
Any help would be greatly appreciated!
9 Replies
it depends on how you want your app to function. if you want the user data to be fetched before the app loads for instance, you could use a middleware
Ah okay, does that still trigger when a user is trying to log in?
It depends on how you design it, but it’s achievable
Okay thanks for the help!
Welcome
Plugin also could be an option
You could try it in "app.vue" (you probably have to create that file, see the docs).
Simplified example of my code (I'm using Nuxt useState as you can see):
That is kinda what I'm doing right now, I found my problem, I was calling my loadUserData in a default.vue layout (which loaded on the login page), after the login, it didn't trigger any more unless I refreshed.
I tried the middleware but then it was triggering twice and I could not get rid of the hydration mismatch.
here's what I normally use to prevent the double call in the middleware