Service worker update

This commit is contained in:
2024-09-18 13:17:04 +02:00
parent af368ad14e
commit 789fb2be15

View File

@@ -135,6 +135,68 @@
document.body.append(scriptTag);
}
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 (navigator.serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
// if there's an existing controller (previous Service Worker), show the prompt
// you can tweak this and delay the notification once the page is load you can show a notification and ask for a new refresh
}
}
else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
<!-- <script defer>
var serviceWorkerVersion = '{{flutter_service_worker_version}}';
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'flutter_bootstrap.js';
scriptTag.type = 'application/javascript';
scriptTag.defer = true;
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
@@ -199,7 +261,7 @@
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
</script> -->
</body>
<!-- File Picker & PDF viewer -->