New to Nuxt and SSR. What I'm trying to do is straightforward: I'm trying to trigger a function before the page loads that will SSR my firestore data. If I add a button with @onclick, it works fine. But when I try to replace the Button/OnClick with @onloadstart or something similar, nothing happens.
What am I missing? I suspect I’m thinking about this through a client side lens. Not a SSR lens.
<template @onloadstart="beforeCreate()">
<section>
<div>
<div v-for="article in articles" id="articleCardList" :key="article">
<v-card elevation="1" outlined>
<div class="d-flex">
<v-avatar class="ma-1" size="125" tile>
<v-img :src="article.thumbnail"></v-img>
</v-avatar>
<div>
<v-card-title
class="text-h5 text-wrap d-flex justify-space-between text-caption"
v-text="article.title"
>
{{ article.title }}
</v-card-title>
</div>
</div>
</v-card>
</div>
</div>
</section>
</template>
<script>
import { fireDb } from '~/plugins/firebase.js'
export default {
data() {
return {
articles: [],
}
},
methods: {
async asyncData() {
this.articles = []
await fireDb
.collection('articles')
.get()
.then((querySnapShot) => {
querySnapShot.forEach((doc) => {
this.articles.push({
id: doc.id,
title: doc.data().title,
description: doc.data().description,
thumbnail: doc.data().thumbnail,
date: doc.data().date,
link: doc.data().link,
source: doc.data().source,
})
console.log(this.articles)
})
})
},
beforeCreate() {
window.addEventListener('beforeCreate', this.asyncData)
},
},
}
</script>
@clickactually. Did you tried it?@clickis client side only tho.fetch()hook and managing a loading state could be nice while fetching the data from firebase. You could also usecreated()but this one will not block the rendering until it's done fetching. Actually, onlyasyncDatadoes but it's just between a page transition hence, if you arrive on this page it will not be handled. Not sure but maybe a middleware could be blocking, need to try it out. Does your event listener work btw?beforeCreate. Does it trigger something at all? Otherwise, I gave you the alternative (fetch()hook).$fetchState.pendingas stated in the docs: nuxtjs.org/docs/2.x/components-glossary/pages-fetch