This commit is contained in:
2025-11-14 12:33:35 +02:00
parent 64443a5076
commit 47ca6d7311

View File

@@ -365,7 +365,7 @@
    var serviceWorkerVersion = '{{flutter_service_worker_version}}';
    var scriptLoaded = false;
    function loadMainDartJs(isUpdate) {
    function loadMainDartJs() {
      console.log('Loading app...');
      if (scriptLoaded) {
        return;
@@ -380,17 +380,6 @@
        onEntrypointLoaded: function (engineInitializer) {
          engineInitializer.initializeEngine().then(function (appRunner) {
            appRunner.runApp();
           
// --- NEW PROMPT LOCATION ---
if (isUpdate) {
// Only show prompt AFTER the old app (the one currently running) has loaded
console.log('New app version installed. Prompting for reload.');
if (confirm('A new version of the app is available. Refresh now to update?')) {
window.location.reload();
}
}
// ---------------------------
          });
        }
      });
@@ -400,7 +389,7 @@
    function getCleanVersion(version) {
      return version.replace(/"/g, '');
    }
    
    if ('serviceWorker' in navigator) {
      var startLoad = new Date();
      // Service workers are supported. Use them.
@@ -410,16 +399,22 @@
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => {
// Flag to track if this registration resulted in an update
let isUpdateInstallation = false;
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener('statechange', () => {
                if (serviceWorker.state == 'activated') {
                  console.log('Installed new service worker.');
                  // Pass the update flag to loadMainDartJs
                  loadMainDartJs(isUpdateInstallation);
                  loadMainDartJs();
                  // Optional: Prompt user to reload for the new version to take effect
                  // The user is currently running the OLD cached version of the app.
                  // This prompt is generally preferred over the previous logic.
                  if (navigator.serviceWorker.controller) {
                    // Only prompt if this isn't the first time loading.
                    console.log('New app version activated. Prompting for reload.');
                    // You can replace this confirm with a better UI notification.
                    if (confirm('A new version of the app is available. Refresh now to update?')) {
                      window.location.reload();
                    }
                  }
                }
              });
            }
@@ -430,27 +425,26 @@
            console.log('Active Service Worker Version: ' + (currentSWVersion || 'None'));
            const isMatch = currentSWVersion === cleanServiceWorkerVersion;
            console.log('Latest Service Worker Installed: ' + isMatch);
            
            if (!reg.active && (reg.installing || reg.waiting)) {
              // First time load: wait for activation.
              console.log('No Service Worker Available - Installing New Service Worker.');
              waitForActivation(reg.installing || reg.waiting);
            } else if (!isMatch) {
              // New version available: force update.
isUpdateInstallation = true; // Set flag when update is initiated
              console.log('New service worker available. Updating and waiting for activation.');
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log('Service Worker up-to-date, Loading app.');
              loadMainDartJs(false); // No update, pass false
              loadMainDartJs();
            }
          })
          .catch((error) => {
              console.error('Service Worker registration failed:', error);
              loadMainDartJs(false); // Fallback if registration fails completely
          });
.catch((error) => {
console.error('Service Worker registration failed:', error);
loadMainDartJs(); // Fallback if registration fails completely
});
        // If service worker doesn't succeed in a reasonable amount of time,
@@ -460,7 +454,7 @@
            console.warn(
              'Failed to load app from a service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs(false);
            loadMainDartJs();
          }
        }, 500);
        var finishLoad = new Date();
@@ -471,12 +465,13 @@
    else {
      // Service workers not supported. Just drop the <script> tag.
      console.log('Service Workers Not Supported. Loading app directly.');
      loadMainDartJs(false);
      loadMainDartJs();
    }
  </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>
<!--------------------->