firebase 托管上的 Flutter 网站适用于域 1 但不适用于域 2
Flutter website on firebase hosting works fine with domain 1 but not domain 2
我正在使用 flutter web 为我的业务创建一个网站。我将它托管在 url
的 firebase 托管上
https://xspectre-9a3b3.web.app/
现在,当我添加新域时
https://xspectre.net
该网站未在此域中加载并给出错误
main.dart.js:1 Uncaught SyntaxError: Unexpected token '<'
(index):1 Uncaught (in promise) DOMException: Failed to register a ServiceWorker for scope ('https://xspectre.net/') with script ('https://xspectre.net/flutter_service_worker.js'): The script has an unsupported MIME type ('text/html').
它也不适用于 https://xspectre-9a3b3.firebaseapp.com/。我该怎么办?
编辑
我发现真正的错误不是 MIME 类型,因为它也在我的真实项目中。实际错误不知何故
Uncaught SyntaxError: Unexpected token '<'
当我打开 main.dart.js 时,它会显示不同的代码。它向我显示 html 文件而不是 main.dart.js 代码,我认为这就是导致问题的原因。我不知道这两个 URL 的文件如何不同或为什么不同。
**编辑 2 **
终于找到了根本问题。
每当我尝试执行 firebase init 时,它都会初始化整个 flutter 目录。这意味着它说,
You're about to initialize a Firebase project in this directory:
C:\FlutterProjects\xspectre
而不是
You're about to initialize a Firebase project in this directory:
C:\FlutterProjects\xspectre\build
现在我需要弄清楚如何在 /build 而不是 root 中执行 firebase init 和 deloy
该网站可能仍然适合您,因为它已经被 service worker 缓存了。换个浏览器试试,不行。
firebase init
应该是 运行 在项目目录下,而不是在构建目录下。
之后,将您的 firebase.json 文件编辑为
{
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
]
}
}
"public": "build/web"
指向部署文件所在目录
现在你可以做 flutter build web
和 firebase deploy
我正在使用 flutter web 为我的业务创建一个网站。我将它托管在 url
的 firebase 托管上
https://xspectre-9a3b3.web.app/
现在,当我添加新域时
https://xspectre.net
该网站未在此域中加载并给出错误
main.dart.js:1 Uncaught SyntaxError: Unexpected token '<'
(index):1 Uncaught (in promise) DOMException: Failed to register a ServiceWorker for scope ('https://xspectre.net/') with script ('https://xspectre.net/flutter_service_worker.js'): The script has an unsupported MIME type ('text/html').
它也不适用于 https://xspectre-9a3b3.firebaseapp.com/。我该怎么办?
编辑
我发现真正的错误不是 MIME 类型,因为它也在我的真实项目中。实际错误不知何故
Uncaught SyntaxError: Unexpected token '<'
当我打开 main.dart.js 时,它会显示不同的代码。它向我显示 html 文件而不是 main.dart.js 代码,我认为这就是导致问题的原因。我不知道这两个 URL 的文件如何不同或为什么不同。
**编辑 2 **
终于找到了根本问题。
每当我尝试执行 firebase init 时,它都会初始化整个 flutter 目录。这意味着它说,
You're about to initialize a Firebase project in this directory:
C:\FlutterProjects\xspectre
而不是
You're about to initialize a Firebase project in this directory:
C:\FlutterProjects\xspectre\build
现在我需要弄清楚如何在 /build 而不是 root 中执行 firebase init 和 deloy
该网站可能仍然适合您,因为它已经被 service worker 缓存了。换个浏览器试试,不行。
firebase init
应该是 运行 在项目目录下,而不是在构建目录下。
之后,将您的 firebase.json 文件编辑为
{
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
]
}
}
"public": "build/web"
指向部署文件所在目录
现在你可以做 flutter build web
和 firebase deploy