increase load
This commit is contained in:
@@ -362,112 +362,113 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<script defer="">
|
<script defer="">
|
||||||
var serviceWorkerVersion = '{{flutter_service_worker_version}}';
|
var serviceWorkerVersion = '{{flutter_service_worker_version}}';
|
||||||
var scriptLoaded = false;
|
var scriptLoaded = false;
|
||||||
function loadMainDartJs() {
|
|
||||||
console.log('Loading app...');
|
|
||||||
if (scriptLoaded) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
scriptLoaded = true;
|
|
||||||
{{flutter_js}}
|
|
||||||
{{flutter_build_config}}
|
|
||||||
_flutter.loader.load({
|
|
||||||
serviceWorker: {
|
|
||||||
serviceWorkerVersion: serviceWorkerVersion,
|
|
||||||
},
|
|
||||||
onEntrypointLoaded: function (engineInitializer) {
|
|
||||||
engineInitializer.initializeEngine().then(function (appRunner) {
|
|
||||||
appRunner.runApp();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('serviceWorker' in navigator) {
|
function loadMainDartJs() {
|
||||||
var startLoad = new Date();
|
console.log('Loading app...');
|
||||||
// Service workers are supported. Use them.
|
if (scriptLoaded) {
|
||||||
window.addEventListener('load', function () {
|
return;
|
||||||
// Wait for registration to finish before dropping the <script> tag.
|
}
|
||||||
// Otherwise, the browser will load the script multiple times,
|
scriptLoaded = true;
|
||||||
// potentially different versions.
|
{{flutter_js}}
|
||||||
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
|
{{flutter_build_config}}
|
||||||
navigator.serviceWorker.register(serviceWorkerUrl)
|
_flutter.loader.load({
|
||||||
.then((reg) => {
|
serviceWorker: {
|
||||||
|
serviceWorkerVersion: serviceWorkerVersion,
|
||||||
|
},
|
||||||
|
onEntrypointLoaded: function (engineInitializer) {
|
||||||
|
engineInitializer.initializeEngine().then(function (appRunner) {
|
||||||
|
appRunner.runApp();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function waitForActivation(serviceWorker) {
|
// Helper function to strip quotes from the version placeholder
|
||||||
serviceWorker.addEventListener('statechange', () => {
|
function getCleanVersion(version) {
|
||||||
if (serviceWorker.state == 'activated') {
|
return version.replace(/"/g, '');
|
||||||
console.log('Installed new service worker.');
|
}
|
||||||
loadMainDartJs();
|
|
||||||
}
|
if ('serviceWorker' in navigator) {
|
||||||
});
|
var startLoad = new Date();
|
||||||
}
|
// Service workers are supported. Use them.
|
||||||
|
window.addEventListener('load', function () {
|
||||||
|
var cleanServiceWorkerVersion = getCleanVersion(serviceWorkerVersion);
|
||||||
|
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + cleanServiceWorkerVersion;
|
||||||
|
|
||||||
console.log('Active Service Worker URL: ' + reg.active.scriptURL);
|
navigator.serviceWorker.register(serviceWorkerUrl)
|
||||||
const currentSWVersion = reg.active.scriptURL.split("=")[1].replaceAll("%22", "");;
|
.then((reg) => {
|
||||||
console.log('Active Service Worker Version: ' + serviceWorkerVersion.replaceAll("\"", ""));
|
function waitForActivation(serviceWorker) {
|
||||||
console.log('Latest Service Worker Version: ' + currentSWVersion);
|
serviceWorker.addEventListener('statechange', () => {
|
||||||
const isMatch = currentSWVersion === serviceWorkerVersion.replaceAll("\"", "");
|
if (serviceWorker.state == 'activated') {
|
||||||
console.log('Latest Service Worker Installed: ' + isMatch);
|
console.log('Installed new service worker.');
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!reg.active && (reg.installing || reg.waiting)) {
|
const currentSWVersion = reg.active ? reg.active.scriptURL.split("=")[1].replaceAll("%22", "") : null;
|
||||||
// No active web worker and we have installed or are installing
|
console.log('Active Service Worker URL: ' + (reg.active ? reg.active.scriptURL : 'None'));
|
||||||
// one for the first time. Simply wait for it to activate.
|
console.log('Latest Service Worker Version: ' + cleanServiceWorkerVersion);
|
||||||
console.log('No Service Worker Available - Installing New Service Worker.');
|
console.log('Active Service Worker Version: ' + (currentSWVersion || 'None'));
|
||||||
waitForActivation(reg.installing || reg.waiting);
|
const isMatch = currentSWVersion === cleanServiceWorkerVersion;
|
||||||
} else if (!isMatch) {
|
console.log('Latest Service Worker Installed: ' + isMatch);
|
||||||
// When the app updates the serviceWorkerVersion changes, so we
|
|
||||||
// need to ask the service worker to update.
|
if (!reg.active && (reg.installing || reg.waiting)) {
|
||||||
console.log('New service worker available.');
|
// First time load: wait for activation.
|
||||||
reg.update();
|
console.log('No Service Worker Available - Installing New Service Worker.');
|
||||||
waitForActivation(reg.installing);
|
waitForActivation(reg.installing || reg.waiting);
|
||||||
} else {
|
} else if (!isMatch) {
|
||||||
// Existing service worker is still good.
|
// New version available: force update.
|
||||||
console.log('Service Worker up-to-date, Loading app.');
|
console.log('New service worker available. Updating and waiting for activation.');
|
||||||
loadMainDartJs();
|
reg.update();
|
||||||
}
|
waitForActivation(reg.installing);
|
||||||
|
} else {
|
||||||
|
// Existing service worker is still good.
|
||||||
|
console.log('Service Worker up-to-date, Loading app.');
|
||||||
|
loadMainDartJs();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.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,
|
|
||||||
// fallback to plaint <script> tag.
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!scriptLoaded) {
|
|
||||||
console.warn(
|
|
||||||
'Failed to load app from a service worker. Falling back to plain <script> tag.',
|
|
||||||
);
|
|
||||||
loadMainDartJs();
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
var finishLoad = new Date();
|
|
||||||
var loadTime = (finishLoad.getTime() - startLoad.getTime());
|
|
||||||
console.log("Load Time: " + loadTime);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// If service worker doesn't succeed in a reasonable amount of time,
|
||||||
|
// fallback to plain <script> tag.
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!scriptLoaded) {
|
||||||
|
console.warn(
|
||||||
|
'Failed to load app from a service worker. Falling back to plain <script> tag.',
|
||||||
|
);
|
||||||
|
loadMainDartJs();
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
var finishLoad = new Date();
|
||||||
|
var loadTime = (finishLoad.getTime() - startLoad.getTime());
|
||||||
|
console.log("Load Time: " + loadTime);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Service workers not supported. Just drop the <script> tag.
|
||||||
|
console.log('Service Workers Not Supported. Loading app directly.');
|
||||||
|
loadMainDartJs();
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
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
|
|
||||||
if (navigator.serviceWorker.controller) {
|
|
||||||
// you have a better UI here, reloading is not a great user experince here.
|
|
||||||
const confirmed = alert('New version of the app is available. Refresh now');
|
|
||||||
if (confirmed == true) {
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Service workers not supported. Just drop the <script> tag.
|
|
||||||
console.log('Service Not Supported.');
|
|
||||||
loadMainDartJs();
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user