service worker v4
This commit is contained in:
@@ -1,50 +1,5 @@
|
||||
{{flutter_js}}
|
||||
{{flutter_build_config}}
|
||||
|
||||
const serviceWorkerVersion = '{{flutter_service_worker_version}}';
|
||||
|
||||
// 1. Let Flutter handle the service worker registration cleanly via its native loader
|
||||
_flutter.loader.load({
|
||||
serviceWorkerSettings: {
|
||||
serviceWorkerVersion: serviceWorkerVersion
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Safely monitor the registration generated by Flutter without double-registering
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready.then((reg) => {
|
||||
// If an updated worker is already waiting in the background
|
||||
if (reg.waiting) {
|
||||
showUpdatePrompt(reg);
|
||||
}
|
||||
|
||||
// Listen for new updates found while the user has the app open
|
||||
reg.addEventListener('updatefound', () => {
|
||||
const newWorker = reg.installing;
|
||||
if (newWorker) {
|
||||
newWorker.addEventListener('statechange', () => {
|
||||
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
showUpdatePrompt(reg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Guard against infinite reload loops when the controller shifts
|
||||
let refreshing = false;
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
if (!refreshing) {
|
||||
refreshing = true;
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function showUpdatePrompt(reg) {
|
||||
if (confirm('A new version of MIH is available. Refresh now to update?')) {
|
||||
reg.unregister().then(function () {
|
||||
window.location.reload(); // Unregister and reload to force clear the browser's cache
|
||||
});
|
||||
}
|
||||
}
|
||||
// This is the default loader that Flutter 3.24 expects
|
||||
_flutter.loader.load();
|
||||
|
||||
@@ -257,6 +257,46 @@
|
||||
</script>
|
||||
|
||||
<script src="flutter_bootstrap.js" async></script>
|
||||
<script>
|
||||
(function () {
|
||||
if ('serviceWorker' in navigator) {
|
||||
// Wait for the service worker to be ready (registered by Flutter)
|
||||
navigator.serviceWorker.ready.then((registration) => {
|
||||
// If an updated worker is already waiting
|
||||
if (registration.waiting) {
|
||||
showUpdatePrompt(registration);
|
||||
}
|
||||
|
||||
// Listen for new updates while the app is open
|
||||
registration.addEventListener('updatefound', () => {
|
||||
const newWorker = registration.installing;
|
||||
if (newWorker) {
|
||||
newWorker.addEventListener('statechange', () => {
|
||||
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
showUpdatePrompt(registration);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Prevent infinite reload loops
|
||||
let refreshing = false;
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
if (!refreshing) {
|
||||
refreshing = true;
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function showUpdatePrompt(reg) {
|
||||
if (confirm('A new version of MIH is available. Refresh now to update?')) {
|
||||
reg.unregister().then(() => window.location.reload());
|
||||
}
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<script id="pdfjs-lib" src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.2.146/pdf.min.js" defer=""></script>
|
||||
<script id="pdfjs-worker" type="text/javascript"
|
||||
src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/3.2.146/pdf.worker.min.js" defer=""></script>
|
||||
|
||||
Reference in New Issue
Block a user