Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

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

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

EDIT

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

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.

**EDIT 2 **

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:FlutterProjectsxspectre

and not

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

  C:FlutterProjectsxspectreuild

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

question from:https://stackoverflow.com/questions/65917731/flutter-website-on-firebase-hosting-works-fine-with-domain-1-but-not-domain-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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" points to the directory where the files for deployment are.

Now you can do flutter build web and firebase deploy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...