firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 915 forks source link

Blank Page displayed Flutter App hosted with Firebase Hosting on rewrite #7390

Closed FixUndFerdig closed 3 days ago

FixUndFerdig commented 3 days ago

[REQUIRED] Environment info

firebase-tools: 13.11.3

Platform: Windows

[REQUIRED] Test case

  1. Create a new Flutter Web Project
  2. Initialize Firebase Hosting
  3. Add rewrite: "source": "**", "destination": "/index.html"
  4. Deploy

[REQUIRED] Steps to reproduce

Set up Firebase Hosting like described in the Documentation. When the Project is deployed 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, a rewrite, like this, has to be added:

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

[REQUIRED] Expected behavior

When this is deployed, it should display my Webpage.

[REQUIRED] Actual behavior

When this is deployed locally with the Emulator Suite it works as intended. But when this is deployed online (with 'firebase deploy'), a blank Page is displayed and the Console on the Browser displays this Messages:

> Rejecting promise with error: FormatException: SyntaxError: Unexpected token '<', "<html>
<h"... 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 src="flutter_bootstrap.js" async></script>
  <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>
</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
aalej commented 3 days ago

Hey @FixUndFerdig, thanks for reaching out. Based on the information you provided, it looks like the issue you’re encountering is the same as https://github.com/firebase/firebase-tools/issues/7378 and https://github.com/firebase/firebase-tools/issues/7363 which affects v13.11.4 and v13.11.3. A fix for this has been implemented in https://github.com/firebase/firebase-tools/pull/7382, which should be available in the next release of firebase-tools. In the meantime, using v13.11.2 should workaround the issue.

aalej commented 3 days ago

Just an update here, it looks like v13.12.0 has just been released, updating to the latest version should fix the issue.

FixUndFerdig commented 3 days ago

Yes, updating fixed the issue. Thank you!