'Failed to register a ServiceWorker (Flutter)

I'm working on a Flutter Web App and today I got these errors:

A bad HTTP response code (404) was received when fetching the script.

Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope ('https://adrieldf.github.io/') with script ('https://adrieldf.github.io/flutter_service_worker.js'): A bad HTTP response code (404) was received when fetching the script.

The only thing "different" that I added to the code was a dependency for the Share library, but I don't think that that is the problem.

Here is the interesting thing, I've got the app hosted on Github Pages and after every commit I have a action that builds the web app. After the build, when I try to access it doesn't load and the errors appears. But building it and running it locally works normally.

Here is the source code if you want to check: https://github.com/Adrieldf/allthewidgets The website link is in the repo description.



Solution 1:[1]

I solved my problem by changing the index.html file

from <base href="/">

to <base href="/directory_name/">

Solution 2:[2]

Looks, like you are trying to run the project from the project_root/web folder.

If you just start a webserver here (for example, by using python -m http.server 8088), the application will start and even will be working partially.

But compiled web app located in project_root/build/web. Try to run your webserver in that folder.

Solution 3:[3]

Solved by changing base tag inside build/web/index.html

<base href="/">

to

<base href="./">

added . (dot)

Solution 4:[4]

Well, the problem is what the error message says. Your JS is trying to register a Service Worker from the script 'https://adrieldf.github.io/flutter_service_worker.js' but that does not exist on the server. If you open that URL it's a 404.

Solution 5:[5]

For me got resolved simply after upgrading flutter packages:

flutter packages upgrade

Solution 6:[6]

flutter create .

flutter build web

and use the project/build/web as server's location

Solution 7:[7]

you can use peanut to build your flutter app and copy it to gh-pages project using something like:

peanut --extra-args "--base-href=/directory-name/"

Solution 8:[8]

Solved by changing base tag inside build/web/index.html

<base href="/">

to added . (dot) with the folder name inside which i added my flutter build

<base href="./flutter/portfolio">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Baltazar Schneider
Solution 2 xuthus
Solution 3 GameDevBhavesh
Solution 4 pate
Solution 5 ego
Solution 6 Ihor Konovalenko
Solution 7 bkbonner
Solution 8 SAMEER