2017-06-01 158 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的預先呈現內容。

相關問題