【问题标题】:Firebase hosting rewrites for Cloud Functions firebase.json not working with multiple environmentsCloud Functions firebase.json 的 Firebase 托管重写不适用于多个环境
【发布时间】:2021-07-03 23:14:51
【问题描述】:

我有一个项目,其中设置了多个 Firebase 托管项目(用于暂存和生产),当我根据 Firebase 文档 here 添加重写时,函数 /bigben 的重写在 https://customdomain.com/bigben 不起作用(两者都部署到生产和模拟器中)

firebase.json

{
  "hosting": [
    {
      "target": "production",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben",
          "function": "bigben"
        },
        {
          "source": "**",
          "destination": "/index.html"
        },
        {
          "source": "!/@(js|css|svg|jpg|png|gif|ico|json|html)/**",
          "destination": "/index.html"
        }
      ]
      // ...
    },
    {
      "target": "staging",
      "public": "build",
      "rewrites": [
        {
          "source": "/bigben",
          "function": "bigben"
        },
        {
          "source": "**",
          "destination": "/index.html"
        },
        {
          "source": "!/@(js|css|svg|jpg|png|gif|ico|json|html)/**",
          "destination": "/index.html"
        }
      ]
      // ...
    }
  ]
}

如何设置 /bigben 以使用此自定义域(已在 Firebase 中配置为托管/前端 create-react-app?我已经看到从构建文件夹中删除 index.html,但不是这样CRA 需要吗?

【问题讨论】:

    标签: firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    我知道文档说您的 source 应该看起来像这样,一条简单的路径,但它似乎也对我不起作用。将您的 source 更改为 bigben:

    "source": "/bigben",

    到这里:

    "source": "/bigben/**",

    之后它应该正确地进行重写。

    编辑:

    此外,您的重写顺序不正确,因此您的所有源文件都被重写为 index.html。以下是我建议的更改:

    {
      "hosting": [
        {
          "target": "production",
          "public": "build",
          "rewrites": [
            {
              "source": "/bigben/**",
              "function": "bigben"
            },
            {
              "source": "!**/*.*",
              "destination": "/index.html"
            }
          ]
          // ...
        },
        {
          "target": "staging",
          "public": "build",
          "rewrites": [
            {
              "source": "/bigben/**",
              "function": "bigben"
            },
            {
              "source": "!**/*.*",
              "destination": "/index.html"
            }
          ]
          // ...
        }
      ]
    }
    

    【讨论】:

    • 谢谢!仍然得到你需要启用 JavaScript 才能运行这个应用程序。 (index.html of public: build) 这样做之后,在重新部署到生产环境和重新启动模拟器之后
    • @user2521295 更新了我的答案
    • 谢谢!会试试这个 - 乱序 - 我认为文档说要在 { source: "**", destination: index.html } 之前添加函数重写?它们应该按什么顺序排列?
    • 问题是您在通配符之后的第三次重写。您的第二个重写语句说将不是/bigbenanything 重写为index.html。它永远不会命中您的第三次重写语句,因此访问者无法下载您的 JS/CSS 资产(我相信,可能是错误的)。
    • 啊,明白了,谢谢!将审查 CSS/JS 资产重写 - 到目前为止还没有问题(因为人们已经能够使用所有正确的样式/脚本访问该站点)但没有尝试直接下载。
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2020-08-19
    • 2018-10-27
    • 2018-10-02
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多