Search code examples
flutterfirebasefirebase-hosting

Blank Page displayed Flutter App hosted with Firebase Hosting on rewrite


I am experiencing an issue with Firebase Hosting of my Flutter App. I have set up Firebase Hosting like described in the Documentation. When I deploy the Project with this firebase.json - file it works like intended.

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

To make URLs like <Hosting-Domain>/download accessible, I have to add a rewrite, like this:

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

When I deploy the Project with the rewrite, it works fine emulated locally with the Firebase Emulator Suite, but when in open the Website online, a blank Page is displayed. The Console displays this Messages:

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

The FormatException apparently refers to my Index.html-file which looks like this:

<!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>

Ways I tried to fix this Error:

  1. Altered the base href in Index.html to "$FLUTTER_BASE_HREF" or “./”
  2. Changed "public": "build/web" in firebase.json to "source": "." and similar combinations
  3. Set up an new Project step-by-step (adding dependencies, source code, …), this failed at the same Step like this
  4. Altered the rewrite statement in firebase.json (different statement, Redirect, …): rewrite is necessary, only fails with rewrite

Solution

  • This was a issue which affected firebase-tools v13.11.3 and v13.11.4. Updating to v13.12.0 (npm update -g firebase-tools) fixed this issue.