在firebase函数中,只对特定的用户代理调用云功能。

问题描述 投票:1回答:1

我想只在请求来自Google bot用户代理时才调用firebase函数。

目前我对每个http请求都调用该函数。

  1. 请求来自
  2. 检查用户代理
  3. 用户代理是 谷歌 然后 response.send("hello google")
  4. 用户代理不是 谷歌 从firebase主机上渲染index.html文件,这样会造成循环,因为函数会从firebase主机上请求index.html文件,这样做会再次调用函数。

request({uri: "http://example.com/index.html"}, 
    function(error, response, body) {
        res.send(body)
    });
});

这将导致一个循环,因为该函数将要求从firebase托管的index.html文件,这样做将再次调用该函数。所以我认为最好是调用该函数,只有当请求来自谷歌用户代理。

有什么办法可以解决这个问题吗,谢谢。

javascript firebase
1个回答
0
投票

Google bot发出的请求有特定的用户代理,几乎所有的用户代理都包含关键字Googlebot。只要得到头,检查它是否包含关键字.假设你使用的是express。

if (req.get('User-Agent').toLowerCase().includes('googlebot')) {
    res.send("hello google")
} else {
    request({uri: "http://example.com/index.html"}, 
        function(error, response, body) {
            res.send(body)
        });
    });
    //or res.redirect(302, "http://example.com/index.html")
}

https:/developers.whatismybrowser.comuseragentsexploresoftware_namegooglebot。https:/support.google.comwebmastersanswer1061943

© www.soinside.com 2019 - 2024. All rights reserved.