imporve loadtine v1

This commit is contained in:
2024-09-20 11:38:45 +02:00
parent b9fea5cb7f
commit c2e80e639f

View File

@@ -120,194 +120,127 @@
</picture> </picture>
<!-- Versioning --> <!-- Versioning -->
<!-- <script src="flutter_bootstrap.js?v0.0.0.40" async></script> --> <script src="flutter_bootstrap.js" defer></script>
<script defer> <script defer>
var serviceWorkerVersion = '{{flutter_service_worker_version}}'; window.addEventListener('load', function () {
var scriptLoaded = false;
function loadMainDartJs() {
console.log('Loading app...');
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) { FlutterLoader.load({
var startLoad = new Date(); serviceWorker: {
// Service workers are supported. Use them. serviceWorkerVersion: serviceWorkerVersion,
window.addEventListener('load', function () { },
// Wait for registration to finish before dropping the <script> tag. onEntrypointLoaded: async function (engineInitializer) {
// Otherwise, the browser will load the script multiple times, let appRunner = await engineInitializer.initializeEngine();
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) { await appRunner.runApp();
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
console.log('Active Service Worker URL: ' + reg.active.scriptURL);
const currentSWVersion = reg.active.scriptURL.split("=")[1];
console.log('Active Service Worker Version: ' + serviceWorkerVersion.replaceAll("\"", ""));
console.log('Latest Service Worker Version: ' + currentSWVersion);
const isMatch = currentSWVersion === serviceWorkerVersion.replaceAll("\"", "");
console.log('Latest Service Worker Installed: ' + isMatch);
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.
console.log('No Service Worker Available - Installing New Service Worker.');
waitForActivation(reg.installing || reg.waiting);
} else if (!isMatch) {
// 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('Service Worker up-to-date, Loading app.');
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 a service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
var finishLoad = new Date();
var loadTime = (finishLoad.getTime() - startLoad.getTime());
console.log("Load Time: " + loadTime);
});
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> </script>
<!-- <script defer> <!--
var serviceWorkerVersion = '{{flutter_service_worker_version}}'; <script defer>
var scriptLoaded = false; var serviceWorkerVersion = '{{flutter_service_worker_version}}';
function loadMainDartJs() { var scriptLoaded = false;
if (scriptLoaded) { function loadMainDartJs() {
return; console.log('Loading app...');
} if (scriptLoaded) {
scriptLoaded = true; return;
var scriptTag = document.createElement('script');
scriptTag.src = 'flutter_bootstrap.js';
scriptTag.type = 'application/javascript';
scriptTag.defer = true;
document.body.append(scriptTag);
} }
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) { if ('serviceWorker' in navigator) {
// Service workers are supported. Use them. var startLoad = new Date();
window.addEventListener('load', function () { // Service workers are supported. Use them.
// Wait for registration to finish before dropping the <script> tag. window.addEventListener('load', function () {
// Otherwise, the browser will load the script multiple times, // Wait for registration to finish before dropping the <script> tag.
// potentially different versions. // Otherwise, the browser will load the script multiple times,
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion; // potentially different versions.
navigator.serviceWorker.register(serviceWorkerUrl) var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
.then((reg) => { navigator.serviceWorker.register(serviceWorkerUrl)
function waitForActivation(serviceWorker) { .then((reg) => {
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, function waitForActivation(serviceWorker) {
// fallback to plaint <script> tag. serviceWorker.addEventListener('statechange', () => {
setTimeout(() => { if (serviceWorker.state == 'activated') {
if (!scriptLoaded) { console.log('Installed new service worker.');
console.warn( loadMainDartJs();
'Failed to load app from a service worker. Falling back to plain <script> tag.', }
); });
}
console.log('Active Service Worker URL: ' + reg.active.scriptURL);
const currentSWVersion = reg.active.scriptURL.split("=")[1];
console.log('Active Service Worker Version: ' + serviceWorkerVersion.replaceAll("\"", ""));
console.log('Latest Service Worker Version: ' + currentSWVersion);
const isMatch = currentSWVersion === serviceWorkerVersion.replaceAll("\"", "");
console.log('Latest Service Worker Installed: ' + isMatch);
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.
console.log('No Service Worker Available - Installing New Service Worker.');
waitForActivation(reg.installing || reg.waiting);
} else if (!isMatch) {
// 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('Service Worker up-to-date, Loading app.');
loadMainDartJs(); loadMainDartJs();
} }
}, 4000); });
});
if (navigator.serviceWorker.state == 'activated') { // If service worker doesn't succeed in a reasonable amount of time,
console.log('Installed new service worker.'); // fallback to plaint <script> tag.
loadMainDartJs(); setTimeout(() => {
// if there's an existing controller (previous Service Worker), show the prompt if (!scriptLoaded) {
// you can tweak this and delay the notification once the page is load you can show a notification and ask for a new refresh console.warn(
if (navigator.serviceWorker.controller) { 'Failed to load app from a service worker. Falling back to plain <script> tag.',
// 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'); loadMainDartJs();
if (confirmed == true) { }
window.location.reload(); }, 4000);
} var finishLoad = new Date();
var loadTime = (finishLoad.getTime() - startLoad.getTime());
console.log("Load Time: " + loadTime);
});
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. }
loadMainDartJs(); else {
} // Service workers not supported. Just drop the <script> tag.
</script> --> console.log('Service Not Supported.');
loadMainDartJs();
}
</script> -->
</body> </body>
<!-- File Picker & PDF viewer --> <!-- File Picker & PDF viewer -->
<!-- <script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js" defer></script>
<script type="text/javascript" defer>
pdfjsLib.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js";
</script> -->
<script id="pdfjs-lib" src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js" defer></script> <script id="pdfjs-lib" src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js" defer></script>
<script id="pdfjs-worker" type="text/javascript" <script id="pdfjs-worker" type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js" defer></script> src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js" defer></script>