6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nuxtでOGPタグをfirestoreから取得で動的にしたい際につまったところ

Last updated at Posted at 2020-01-25

やったこと

  • OGPタグの画像URLをfirestoreから取得して動的にした

仕様全体像

  • nuxt×firestore
  • defaultはnuxt.config.jsで指定。個別ページにcomponentのheadプロパティで上書き
  • firestoreから取得したtitleをURLに入れる
  • OGPのURLはcloudinaryのdynamicURLを使用
  • dataとasyncDataを用意し、awaitでfirestoreからtitleを取得
  • componentのheadプロパティでog:imageをset

コード

  data () {
    return {
      room_title: '',
    }
  },
  async asyncData ({ route }) {
    let doc_room_title
    /*room情報取得*/
    var roomRef = db.collection('room').doc(route.params.id)
    await roomRef.get().then(doc => {
      doc_room_title = doc.data().room_title
    })
    return {
      room_title: doc_room_title,
    }
  },
  head () {
    return {
      meta: [
        { hid: 'og:image', property: 'og:image', content: `hogehoge/image/upload/${this.room_title}/hogehoge.png` }
      ]
    }
  },

はまりどころ

SSRでtwitterにogp情報を渡す場合レンダリングタイミングが難しい

twitterが読み込めるようにSSRをしたんだがレンダリングタイミングが難しかった。

レンダリング直前のbeforeCreateでいけんだろと書いてたけど取得できず。beforeCreateでは無理だったのでasyncDataでとってきたが取得する前にレンダリングしてたのか空になっていた。そのためawaitで記述。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?