What is the exact difference between fetch and async data. The official documentation says the following:
asyncData
You may want to fetch data and render it on the server-side. Nuxt.js adds an asyncData method that lets you handle async operations before setting the component data.
asyncData is called every time before loading the component (only for page components). It can be called from the server-side or before navigating to the corresponding route. This method receives the context object as the first argument, you can use it to fetch some data and return the component data.
Fetch
The fetch method is used to fill the store before rendering the page, it's like the asyncData method except it doesn't set the component data. The fetch method, if set, is called every time before loading the component (only for page components). It can be called from the server-side or before navigating to the corresponding route.
The fetch method receives the context object as the first argument, we can use it to fetch some data and fill the store. To make the fetch method asynchronous, return a Promise, nuxt.js will wait for the promise to be resolved before rendering the component.
Fetch is been used to fill the store with data? But in asyncData is this also possible to commit trough a store? I don't understand why there are two methods for.
Both methods are running server-side on the initial load, after that when you navigate through the applicatie it runs client side.
Can someone explain me the advantage of use these methods above the other?
Thanks for help.
Let me re-iterate few points as a pretext to what i'm going to say
asyncData
can set component level objects and access vuex storefetch
cannot set component level objects but has access to vuex storeasyncData
& fetch
will be triggered in server side during initial loadasyncData
and fetch
will be triggered when the corresponding page routes are invoked1) if your design is
then use fetch
2) if your design is
then use asyncData
Can someone explain me the advantage of use these methods above the other?
i don't see any drawbacks in using asyncData
or fetch
Choosing asyncData
or fetch
totally depends on your architecture
Several points mentioned in the answer no longer apply when using newer NuxtJS versions (>= 2.12). Official RFC announcement here.
A good explanation of the new behaviour and differences between asyncData
and the new fetch
can be found in this post in the NuxtJS official blog.
As for choosing between both, I believe the original answer still applies:
i don't see any drawbacks in using
asyncData
orfetch
Choosing
asyncData
orfetch
totally depends on your architecture
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With