![](/img/trans.png)
[英]How to access a function from inside a firebase function that is inside another function
[英]How to access Firebase Hosting project target ID from inside function
我有一个多站点 Firebase 托管设置,它运行良好,为 static 内容和动态元标记提供服务。 我目前正在检查引用者以读取正确的索引文件,但它不可靠并且不适用于 localhost。
我可以从firebase.json
内部的 firebase.json 访问target
属性吗?
import * as functions from 'firebase-functions';
const fs = require('fs');
const url = require('url');
exports.injectMeta = functions.https.onRequest((req, res) => {
const URL = url.parse(req.url);
// HOW CAN I GE THE CURRENT TARGET ID FROM FIREBASE.JSON?
let prefix = 'tam';
if (URL.host === 'example1.com') { // this is not reliable and doesn't work with localhost
prefix = 'foo';
} else if (URL.host === 'example2.com') {
prefix = 'bar';
} else if (URL.host === 'example3.com') {
prefix = 'woo';
}
const template = fs.readFileSync(`./build/${prefix}/index.html`, 'utf8');
let meta = '';
if (URL.pathname.indexOf('/zoo/') === 0 ||
URL.pathname.indexOf('/zaz/') === 0) {
meta += `<meta property="og:url" content="https://api.example.com/sharer${URL.pathname}" />`;
}
meta += `<meta property="fb:app_id" content="XXXX" />
<meta property="og:type" content="other dynamic stuff" />`;
res.status(200).send(template.replace("<head>", "<head>" + meta));
});
我的 firebase.json 看起来像
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
"hosting": [
{
"target": "tam",
"public": "functions/build/tam",
"rewrites": [
{
"source": "/group/**",
"function": "injectMeta"
},
{
"source": "/marketplace/**",
"function": "injectMeta"
},
{
"source": "/faqs/**",
"function": "injectMeta"
},
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**/*.@(jpg|jpeg|gif|png)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=7200"
}
]
},
{
"source": "404.html",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=300"
}
]
}
],
"ignore": [
"**/.*"
]
},
{
"target": "ftb",
"public": "functions/build/ftb",
"rewrites": [
{
"source": "/group/**",
"function": "injectMeta"
},
{
"source": "/marketplace/**",
"function": "injectMeta"
},
{
"source": "/faqs/**",
"function": "injectMeta"
},
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**/*.@(jpg|jpeg|gif|png)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=7200"
}
]
},
{
"source": "404.html",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=300"
}
]
}
],
"ignore": [
"**/.*"
]
},
...
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.