Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase重写函数的路径不工作_Javascript_Reactjs_Firebase_Google Cloud Functions_Firebase Hosting - Fatal编程技术网

Javascript Firebase重写函数的路径不工作

Javascript Firebase重写函数的路径不工作,javascript,reactjs,firebase,google-cloud-functions,firebase-hosting,Javascript,Reactjs,Firebase,Google Cloud Functions,Firebase Hosting,我想将动态元标记添加到index.html,该应用程序是使用create react应用程序创建的,并托管在firebase主机上。我指的是这里的帖子: 我创建了一个新的云功能: const fs = require('fs'); const functions = require('firebase-functions'); exports.host = functions.https.onRequest((req, res) => { const userAgent = req.h

我想将动态元标记添加到index.html,该应用程序是使用create react应用程序创建的,并托管在firebase主机上。我指的是这里的帖子:

我创建了一个新的云功能:

const fs = require('fs');
const functions = require('firebase-functions');

exports.host = functions.https.onRequest((req, res) => {
 const userAgent = req.headers['user-agent'].toLowerCase();
 let indexHTML = fs.readFileSync('./hosting/index.html').toString();
 const path = req.path ? req.path.split('/') : req.path;
 const ogPlaceholder = '<meta name="functions-insert-dynamic-meta">';
 indexHTML = indexHTML.replace(ogPlaceholder, getOpenGraph());
 console.log(indexHTML);
 res.status(200).send(indexHTML);
});

const defaultDesc = 'Test Desc';
const defaultTitle = 'Test Title';
const defaultLogo = 'http://test-domain.com/logo.png';

const getOpenGraph = () => {
 let og = `<meta property="fb:app_id" content="123123123" />`;
 og += `<meta property="og:type" content="website" />`;
 og += `<meta property="og:title" content="${defaultTitle}" />`;
 og += `<meta property="og:description" content="${defaultDesc}" />`;
 og += `<meta property="og:image" content="${defaultLogo}" />`;
 og += `<meta property="og:url" content="https://gifmos-frontend-beta.firebaseapp.com/" />`;
 return og;
};

现在的预期结果是:HTML应该已经被上面编写的函数中的动态元标记所取代。但它似乎不起作用。调用云函数会按预期返回值:但我们需要它来处理“index.html”文件调用,因此我们可以根据请求的页面添加动态OG标记。

如果您的公共目录中有一个静态的
index.html
,只需删除它即可

但是,如果您没有看到自定义页面,请尝试在浏览器中(在chrome中)硬重新加载并清空缓存


Firebase使用
index.html
(如果公用目录中有),并忽略相应路径/url中的函数。

他们接受它作为答案。我猜你(@Vipul Limbachiya)也面临着同样的问题
{
 "hosting": {
 "public": "build",
 "ignore": [
 "firebase.json",
 "**/.*",
 "**/node_modules/**"
 ],
 "rewrites": [
 {
 "source": "**",
 "function": "host"
 }
 ]
 }
}