我的 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>
我尝试修复此错误的方法:
"$FLUTTER_BASE_HREF"
或 “./”
"public": "build/web"
更改为 "source": "."
和类似组合此问题影响了 firebase-tools v13.11.3 和 v13.11.4。更新到 v13.12.0 (
npm update -g firebase-tools
) 修复了此问题。