I updated Nuxt to the latest version because they introduced the new fetch()
hook.
In my project, I'm retrieving data from Prismic. Before, I was using asyncData()
, but when navigating, it took some time before rendering the pages where asyncData()
was invoked (this is how it works).
The ideal solution is to navigate on that particular page, and show a loading animation while retrieving the data.
The new fetch()
hook seems appropriate because it exposes the $fetchState.pending
in order to check the status of the operation.
Now, the code (I'm retrieving subcategories from the category in a shop):
data(){
return{
docs: []
}
},
async fetch() {
try{
const res = await this.$prismic.api.query(this.$prismic.predicates.at('my.category.uid', this.$route.params.sub))
const el = res.results[0]
const query = await this.$prismic.api.query([this.$prismic.predicates.at('document.type','sub-category'), this.$prismic.predicates.at('my.sub-category.category', el.id)], { orderings: '[my.sub-category.position, my.sub-category.title]' })
this.docs = query.results
}
catch (e) {
console.log(e)
}
}
This works at least client side. Will this work for proper SSR? I mean, in asyncData()
(which is invoked server side), this
is not accessible, nor the data()
. If this is the right solution, what's the deal for using asyncData()
over fetch()
anymore?