简体   繁体   English

firebase 上的 Flutter 网站托管在域 1 上运行正常,但在域 2 上运行不正常

[英]Flutter website on firebase hosting works fine with domain 1 but not domain 2

I am using flutter web to create a website for my buisness.我正在使用 flutter web 为我的企业创建一个网站。 I am hosting it on firebase hosting with the url我将其托管在 firebase 上,托管地址为 url
https://xspectre-9a3b3.web.app/ https://xspectre-9a3b3.web.app/
Now when I added the new domain现在,当我添加新域时
https://xspectre.net https://xspectre.net
The website does not load in this domain and gives the error该网站未在此域中加载并给出错误

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').

It does not work with https://xspectre-9a3b3.firebaseapp.com/ either.它也不适用于https://xspectre-9a3b3.firebaseapp.com/ What should I do?我应该怎么办?

EDIT编辑

I discovered the real error is not the MIME type one since it is in my real project too.我发现真正的错误不是 MIME 类型,因为它也在我的真实项目中。 The actual error is somehow实际错误不知何故

Uncaught SyntaxError: Unexpected token '<'

When I open the main.dart.js it shows me different code then it should.当我打开 main.dart.js 时,它会显示不同的代码。 It shows me the html files not the main.dart.js code and I think that is what is causing the problem.它向我显示了 html 文件而不是 main.dart.js 代码,我认为这就是导致问题的原因。 I dont know how or why the files are different for both the URLs.我不知道这两个 URL 的文件如何不同或为什么不同。

**EDIT 2 ** **编辑2 **

Finally figured out the root problem.终于找到了根本问题。
Whenever I try to do firebase init, it goes to initialize the whole flutter directory.每当我尝试执行 firebase init 时,它都会初始化整个 flutter 目录。 This means that it says,这意味着它说,

You're about to initialize a Firebase project in this directory:

  C:\FlutterProjects\xspectre

and not并不是

You're about to initialize a Firebase project in this directory:

  C:\FlutterProjects\xspectre\build

Now I need to figure out how to do firebase init and deloy in the /build and not root现在我需要弄清楚如何在 /build 而不是 root 中执行 firebase init 和 deloy

The website probably still works for you as it is already cached by the service worker.该网站可能仍然适合您,因为它已经被服务人员缓存。 Try with another browser and it will not work.尝试使用其他浏览器,它将无法正常工作。

firebase init should be run in the project directory, not in the build directory. firebase init应该在项目目录中运行,而不是在构建目录中。

After that, edit your firebase.json file to look like之后,编辑您的 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" points to the directory where the files for deployment are. "public": "build/web"指向部署文件所在的目录。

Now you can do flutter build web and firebase deploy现在您可以执行flutter build webfirebase deploy

I solved.我解决了。 I changed <base href="/"> to <base ="https://example.com/" target="_blank"> in web/index.html and ran flutter build web .我在web/index.html <base href="/">更改为<base ="https://example.com/" target="_blank">并运行flutter build web

So, this way, you need to make a DNS redirection like www.example.com to example.com .因此,通过这种方式,您需要进行 DNS 重定向,例如www.example.comexample.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM