I'm using the async fetch hook to render the component in SSR and making the API call inside the same. But on the live server, it is actually loading one more time on the client-side and making one more call to API and as API isn't exposed during client-side so data just washes away and the array object gets again set to empty.
data() {
return {
errored: false,
randomData: [],
};
},
async fetch() {
await this.$axios
.get(this.routeUrl)
.then((res) => {
if (res.data.length == 0 || res.status != 200) this.errored = true;
this.randomData = res.data;
})
.catch((err) => {
console.log(err);
});
},
fetchOnServer: true,
I want to persist this randomData variable, so there shouldn't be another call on the client-side to populate the same.
ssr: trueandtarget: 'server', right? Also, what is your end goal? Not having the request on the client side, only on the server? Because, the default behavior of an universal app, is to hydrate the content once you're on the client-side, hence triggering any hook a second time. Something like this?randomData: []on your client-side. If you want to get some data on the server and pass it directly to your client, you could use nuxtServerInit Vuex action. More details in the lifecycle.