I have written a flutter web app (using HTML-renderer) and it works pretty smoothly when running it from localhost. But if I upload it to the web server (company-wise unfortunately MS IIS, locally I use nginx) behind https, the service worker never gets activated. Here is where I am so far in my analysis:
The index.html is (mostly) autogenerated by flutter. The relevant JS-code that invokes the service worker is
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing ?? reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
From localhost, waitForActivation is invoked 3 times with 'installed', 'activating' and 'activated' and everything works as expected. Clearing the cache I see that a new service worker is installed. Like I said, I did not change the standard index.html expect for adding a loading spinning wheel.
Behind the domain, the waitForActivation-function is called. Then, assets are loaded, even such that are not needed, eg the assets/NOTICE-file. This file is not loaded if I load it from localhost and it also is not mentioned in the AssetsManifest.json. From behind the company domain, the file causes a 404 (might be a problem with the IIS server configuration; but like I said, this file should not even be loaded), and then further downloads are canceled and I run into the 4-second timeout. After that, the page is loaded, though very unreliably (main.dart.js is very often not fully loaded).
If you need more details or maybe have a suggestion how to deactivate the service worker (it is an online-only-app) while still showing a spinning wheel while loading, please tell me. Also apologies if there are some details missing, I am not a Web Designer or that fluent in JS, neither in server configuration.