【问题标题】:invoke cloud function only for certain user agents in firebase functions仅在 firebase 函数中为某些用户代理调用云函数
【发布时间】:2020-08-17 05:00:51
【问题描述】:

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

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

  1. 请求进来
  2. 检查用户代理
  3. 用户代理是 Google 然后是 response.send("hello google")
  4. 用户代理不是 Google 渲染来自 firebase 托管的 index.html。

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

这将导致循环,因为该函数将从 Firebase 托管请求 index.html 文件,并且这样做将再次调用该函数。所以我认为只有当请求来自谷歌用户代理时才调用函数更好

这可能吗?有什么想法可以解决这个问题吗?谢谢!

【问题讨论】:

  • 重定向到 example.com/index.html 不起作用?
  • 会再次触发函数

标签: javascript firebase google-cloud-functions


【解决方案1】:

Google bot 使用特定用户代理发出请求,几乎所有这些请求都包含关键字 Googlebot。只需获取标题并检查它是否包含关键字。 假设您使用的是快递:

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.com/useragents/explore/software_name/googlebot/ https://support.google.com/webmasters/answer/1061943

【讨论】:

  • 这就是我现在正在做的,但我只想在用户代理包含 google 时调用该函数。也按照你的建议做会导致循环
猜你喜欢
  • 2019-01-18
  • 2021-09-13
  • 2021-12-13
  • 2020-10-18
  • 1970-01-01
  • 2020-01-28
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多