Firebase 文档的 Cloud Functions here声明这可以使用云功能来完成 -
Prerendering for single page apps to improve SEO. This allows you to create dynamic meta tags for sharing across various social networks.
我有两个问题:
谁能举例说明预渲染是如何实现的?
这如何与 Firebase 托管结合使用?假设我在
xyz.com/salon/43
上有一个网页,在 Firebase 托管中我有一个 salon.html 是为了响应这个请求而提供的。现在为了能够预呈现,我应该从托管转移到呈现网页的云功能吗?换句话说,我是从"rewrites": [{ "source": "/salon/*", "destination": "/salon.html"}]
到
"rewrites": [{ "source": "/salon", "function": "salon"}]
最佳答案
两个任务: - 将函数添加到您的托管重写中,如您的示例所示 - 编写生成html页面的函数
This tutorial提供了一个很好的示例,以下函数作为来自较长代码段的示例:
const admin = require('firebase-admin');
function buildHtmlWithPost (post) {
const string = '<!DOCTYPE html><head>' \
'<title>' + post.title + ' | Example Website</title>' \
'<meta property="og:title" content="' + post.title + '">' \
'<meta property="twitter:title" content="' + post.title + '">' \
'<link rel="icon" href="https://example.com/favicon.png">' \
'</head><body>' \
'<script>window.location="https://example.com/?post=' + post.id + '";</script>' \
'</body></html>';
return string;
}
module.exports = function(req, res) {
const path = req.path.split('/');
const postId = path[2];
admin.database().ref('/posts').child(postId).once('value').then(snapshot => {
const post = snapshot.val();
post.id = snapshot.key;
const htmlString = buildHtmlWithPost(post);
res.status(200).end(htmlString);
});
};
关于firebase - 如何使用 Cloud Functions for Firebase 为 SEO 预渲染页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44301989/