I’m using Nuxt which does both static site generation (SSG) and server-side rendering (SSR)
My app hits a graphQL API for content using Apollo smart queries (@nuxtjs/apollo).
Before the API returns I can safely use
{{post}}
Without an errror, but if I do for example
{{post.author.name}}
The app errors with can’t read property of null
due to the moment before the API returns.
While this can easily be fixed with either
<template v-if="post && post.author && post.author.name">
{{post.author.name}}
</template>
Or
{{(post && post.author && post.author.name ? post.author.name : '')}}
…Both of these cause a blink during page load.
Because of SSG, the correct content is already in the DOM. Hydration should have already bound the DOM element to some data. By using either of the above, I’m telling Nuxt to immediately throw out the initial data until the content API has returned. In effect I get a sub-second page-load of the correct content followed by a blank until the API request returns a second or so later.
What I actually want to achieve is this
{{(post && post.author && post.author.name)
? post.author.name
: [use what is already in the DOM]}}
or possibly, defer SSR until the Apollo smart query has returned.
How do I handle this momentary lack of data without rendering a blank?