I am currently developing in Nuxt.js and like most beginners, I wanted to know the best lifecycle hook to place API calls. Many resources I have found, much like the one below, state that the created()
hook is the best place for fetching data from an API prior to having everything be loaded.
Difference between the created and mounted events in Vue.js
My question came in when I noticed on the networking tab in developer options that my API within the created()
hook was being called twice. After looking into this further, it states that this hook runs on server side and client side. I notice that mounted()
only runs on client side so I am learning towards utilizing that hook. I did however notice that I can use some if
logic (if process.server) in the created()
hook to only have this run on the client / server and not both. Is this a common solution?
To clarify my question further, if created()
runs on both server and client side, why put my API calls in this hook?