简体   繁体   English

如何使用Cloud Functions for Firebase检测浏览器类型?

[英]How to detect browser type with Cloud Functions for Firebase?

Inside a Cloud Function, I would like to serve different pages for mobile and desktop. 在Cloud Function内部,我想为移动设备和台式机提供不同的页面。 I would like to know : 我想知道 :

  1. if its mobile or desktop (or a good guess) that want this page 如果其移动设备或台式机(或很好的猜测)需要此页面
  2. If possible to get the browser/Google default language 如果可能获得浏览器/ Google默认语言

The goal is to serve different users with different urls. 目的是为不同的用户提供不同的URL。

     exports.contentServer = functions.https.onRequest((request, response) => {

     //determine here if its mobile or not

You can use the following npm package: 您可以使用以下npm软件包:

https://www.npmjs.com/package/detect-browser https://www.npmjs.com/package/detect-browser

Install it by executing in the terminal: 通过在终端中执行安装:

npm i detect-browser

Then you can do for example: 然后,您可以执行以下操作:

const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
if (browser) {
  console.log(browser.name);
  console.log(browser.version);
  console.log(browser.os);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM