Firebase托管上的Flutter网站可以在域1上正常运行,但不能在域2上正常运行 [英] Flutter website on firebase hosting works fine with domain 1 but not domain 2

查看:68
本文介绍了Firebase托管上的Flutter网站可以在域1上正常运行,但不能在域2上正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter Web创建一个商务网站.我使用url
将其托管在Firebase托管上 https://xspectre-9a3b3.web.app/
现在,当我添加新域时
https://xspectre.net
该网站未在此域中加载并显示错误

I am using flutter web to create a website for my buisness. I am hosting it on firebase hosting with the url
https://xspectre-9a3b3.web.app/
Now when I added the new domain
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').

它不适用于 https://xspectre-9a3b3.firebaseapp.com/.我该怎么办?

It does not work with https://xspectre-9a3b3.firebaseapp.com/ either. What should I do?

编辑

我发现真正的错误不是MIME类型一,因为它也在我的真实项目中.实际错误是某种原因

I discovered the real error is not the MIME type one since it is in my real project too. The actual error is somehow

Uncaught SyntaxError: Unexpected token '<'

当我打开main.dart.js时,它向我显示了不同的代码.它向我显示了html文件而不是main.dart.js代码,我认为这是导致问题的原因.我不知道两个URL的文件如何或为何不同.

When I open the main.dart.js it shows me different code then it should. It shows me the html files not the main.dart.js code and I think that is what is causing the problem. I dont know how or why the files are different for both the URLs.

**编辑2 **

最后弄清了根本问题.
每当我尝试执行firebase init时,它都会初始化整个flutter目录.这就是说,

Finally figured out the root problem.
Whenever I try to do firebase init, it goes to initialize the whole flutter directory. This means that it says,

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初始化和部署

Now I need to figure out how to do firebase init and deloy in the /build and not root

推荐答案

该网站可能仍对您有用,因为服务工作者已经对其进行了缓存.尝试使用其他浏览器,它将无法正常工作.

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初始化应该在项目目录中运行,而不是在构建目录中运行.

firebase init should be run in the project directory, not in the build directory.

然后,编辑您的firebase.json文件,使其看起来像

After that, edit your firebase.json file to look like

{
  "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" 指向要部署的文件所在的目录.

"public": "build/web" points to the directory where the files for deployment are.

现在您可以进行 flutter构建网络 firebase部署

这篇关于Firebase托管上的Flutter网站可以在域1上正常运行,但不能在域2上正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆