繁体   English   中英

如何从 function 内部访问 Firebase 托管项目目标 ID

[英]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": [
                "**/.*"
            ]
        },
        ...
    ]
}

我建议在部署 function 时将其添加为环境变量 这些用于配置 function 部署,因此您无需硬编码任何内容。 您还应该检查自动填充的值是否是您可能想要使用的东西。

暂无
暂无

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

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