2017-06-01 161 views
1

的火力地堡文档here云功能指出,这是可以做到使用云功能 -如何使用Firebase的云端功能预渲染SEO页面?

预先转译为单页的应用程序,以改善搜索引擎优化。这使您可以创建动态元标签,以便在各种社交网络上共享。

有2个问题,我有:

  • 有人可以用一个例子说明预渲染是如何实现的?

  • 这是如何与Firebase主机配合使用的?假设我有一个网页xyz.com/salon/43,并且在Firebase托管中我有一个salon.html,它是响应此请求而提供的。现在为了能够预渲染,我应该从托管移动到呈现网页的云端功能?换句话说我去从

    "rewrites": [{ 
        "source": "/salon/*", 
        "destination": "/salon.html"}] 
    

    "rewrites": [{ 
        "source": "/salon", "function": "salon"}] 
    

回答

2

两个任务: - 功能添加到您的托管改写为您的例子 - 编写函数生成一个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); 
    }); 
}; 
1

你是正确的,你重写有效应用的HTML页面指向一个函数,而不是一个静态的文档。然后,当访问该页面时,你的函数将有效地生成将被发送回浏览器的HTML。您现在正借此机会决定HTML的内容应该是什么。

如果内容不需要对每一个连接(每一个根据对pricing page显示的计费费率花钱)产生的,你也可能想利用caching消除服务缓存来自Firebase托管CDN的预先呈现内容。

相关问题