nickfrosty / nuxt-seo

SEO / HTML meta tags module for NuxtJS
https://www.npmjs.com/package/nuxt-seo
MIT License
18 stars 6 forks source link

So what to do with the data from the api? #13

Closed Huucong95 closed 1 year ago

Huucong95 commented 2 years ago

Reason to "not set SEO via asyncData"

The reason for the documentation stating to NOT set the SEO in asyncData for SSR is that the titles will not be updates as you navigate between the different pages on the Nuxt site. In this case the title is only updates upon initial load of the site. And will not change when navigating to another page. But if you refresh the browser on that new page, the title is updated

So what to do with the data from the api?

Huucong95 commented 2 years ago

https://nuxt-seo.frostbutter.com/examples/blog If I do it this way, I need to reload the page to load the 'head'

eliascarella commented 1 year ago

For my SSG I'm using the head method as specified in the documentation: https://nuxt-seo.frostbutter.com/usage. It works perfectly this way ! Just get the api data you need with asyncData and then use it.

async asyncData({ $graphql }) {
    const query = backgroundImg
    const answer = await $graphql.default.request(query)
    return { answer }
  },
head({ $seo }) {
    return $seo({
      description: this.answer.seoDescription,
      }
    })

This code assumes you get an object from your API with a seoDescription data inside. I hope this helps!

nickfrosty commented 1 year ago

@eliascarella answered this so wonderfully, I am going to close this issue :1st_place_medal: