重写时显示由 Firebase 托管托管的 Flutter 应用程序的空白页面

问题描述 投票:0回答:1

我的 Flutter 应用程序的 Firebase 托管遇到问题。 我已经按照文档中的描述设置了 Firebase 托管。当我使用这个 firebase.json 文件部署项目时,它的工作原理如下。

...
"hosting": {
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "frameworksBackend": {
      "region": "europe-west1"
    }
...

为了使像

<Hosting-Domain>/download
这样的 URL 可以访问,我必须添加重写,如下所示:

...
"hosting": {
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "frameworksBackend": {
      "region": "europe-west1"
    },
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
...

当我通过重写部署项目时,它可以使用 Firebase 模拟器套件在本地模拟正常工作,但是当在线打开网站时,会显示一个空白页面。控制台显示此消息:

> Rejecting promise with error: FormatException: SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
> Uncaught (in promise) null

FormatException 显然是指我的 Index.html 文件,如下所示:

<!DOCTYPE html>
<html>
<head>
  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

    This is a placeholder for base href that will be replaced by the value of
    the `--base-href` argument provided to `flutter build`.
  -->
  <base href="/">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter project.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="flutter_application_2">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>flutter_application_2</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
    import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-analytics.js";
    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries
  
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
      // My Firebase-Config
    };
  
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
  </script>
  <script src="flutter_bootstrap.js" async></script>
</body>
</html>

我尝试修复此错误的方法:

  1. 将 Index.html 中的基本 href 更改为
    "$FLUTTER_BASE_HREF"
    “./”
  2. 将 firebase.json 中的
    "public": "build/web"
    更改为
    "source": "."
    和类似组合
  3. 逐步建立一个新项目(添加依赖项、源代码等),这在同一步骤中失败了
  4. 更改了 firebase.json 中的重写语句(不同的语句、重定向等):重写是必要的,只有重写才会失败
flutter firebase firebase-hosting
1个回答
0
投票

此问题影响了 firebase-tools v13.11.3 和 v13.11.4。更新到 v13.12.0 (

npm update -g firebase-tools
) 修复了此问题。

© www.soinside.com 2019 - 2024. All rights reserved.