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>
<!-- Versioning -->
<!-- <script src="flutter_bootstrap.js?v0.0.0.40" async></script> -->
<script src="flutter_bootstrap.js" defer></script>
<script defer>
var serviceWorkerVersion = '{{flutter_service_worker_version}}';
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);
}
window.addEventListener('load', function () {
if ('serviceWorker' in navigator) {
var startLoad = new Date();
// 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) => {
FlutterLoader.load({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
},
onEntrypointLoaded: async function (engineInitializer) {
let appRunner = await engineInitializer.initializeEngine();
function waitForActivation(serviceWorker) {
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();
}
await appRunner.runApp();
}
}
}
else {
// Service workers not supported. Just drop the <script> tag.
console.log('Service Not Supported.');
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);
<!--
<script defer>
var serviceWorkerVersion = '{{flutter_service_worker_version}}';
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) {
// 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 ('serviceWorker' in navigator) {
var startLoad = new Date();
// 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) => {
// 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.',
);
function waitForActivation(serviceWorker) {
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();
}
}, 4000);
});
});
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();
}
// 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.
loadMainDartJs();
}
</script> -->
}
else {
// Service workers not supported. Just drop the <script> tag.
console.log('Service Not Supported.');
loadMainDartJs();
}
</script> -->
</body>
<!-- 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-worker" type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js" defer></script>