Vue.jsでOGPを動的に変えようとしています。
以前までCloud FunctionsやCloud Storageのリージョンが東京になっていなかったので、変更するためにFirebaseのプロジェクトを作り直しました。
すると以前まで効いていたOGPの動的変更が効かなくなってしまいました。恐らく以前とソースは変えていないと思うのですがなぜ効かなくなったか分かる方がおられましたら教えて頂けると助かります。
OGPの動的変更は以下のサイトを参考に作成しております。
https://salary.katsulabo.com/vuejs-firebase-ogp/
/project/〇〇〇〇というURLにアクセスしてもCloud Functionsのログに何も残っていない(shareProjectがトリガーされていない?)ので、rewriteの部分が上手く効いていないのかと思うのですが、なぜか分かりません。
何卒よろしくお願い致します。
// firebase.json // { "functions": { "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run lint" ], "source": "functions" }, "hosting": { "public": "dist", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "/project/*", "function": "shareProject" }, { "source": "**", "destination": "/index.html" } ] }, "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "storage": { "rules": "storage.rules" } }
// index.js // const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); const db = admin.firestore(); function buildHtmlWithPost(id, obj) { if(obj.image === '/img/projectNoImage.png'){ return `<!DOCTYPE html><head> <title>${obj.title}</title> <meta property="og:title" content="${obj.title}"> <meta property="og:image" content="https://SITE_NAME/img/projectNoImage.png"> <meta name="twitter:card" content="summary"> <meta name="twitter:title" content="${obj.title}"> <meta name="twitter:description" content="${obj.content}"> <meta name="twitter:image" content="https://SITE_NAME/img/projectNoImage.png"> <link rel="canonical" href="/project/${id}"> </head> <body> <script>window.location="/project?id=${id}";</script> </body> </html>` }else{ return `<!DOCTYPE html><head> <title>${obj.title}</title> <meta property="og:title" content="${obj.title}"> <meta property="og:image" content="${obj.image}"> <meta name="twitter:card" content="summary"> <meta name="twitter:title" content="${obj.title}"> <meta name="twitter:description" content="${obj.content}"> <meta name="twitter:image" content="${obj.image}"> <link rel="canonical" href="/project/${id}"> </head> <body> <script>window.location="/project?id=${id}";</script> </body> </html>` } } //////////////////////////////////////////// // OGPを動的に変更 /////////////////////////// /////////////////////////////////////////// exports.shareProject = functions .region('asia-northeast1') .https.onRequest(function(req, res) { //URLからidを抽出 const path = req.path.split('/') const projectId = path[path.length-1] //idをキーにfirestoreからデータを取得 db.collection('projects').doc(projectId).get() .then((doc) => { let obj = doc.data() //返却用のHTMLを取得 const htmlString = buildHtmlWithPost(projectId, obj) res.set('Cache-Control', 'public, max-age=300, s-maxage=600') res.status(200).end(htmlString) }).catch((err) => { res.status(500).end(err) }) })
あなたの回答
tips
プレビュー