我正在使用 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
答案 0 :(得分:4)
该网站可能仍然适用于您,因为它已被 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