1
0
mirror of https://github.com/zumbiepig/MineXLauncher.git synced 2025-06-08 09:24:48 +00:00
This commit is contained in:
zumbiepig 2024-08-20 07:35:26 -07:00
parent 3e84050771
commit 020d35d435
4 changed files with 84 additions and 26 deletions

View File

@ -1,5 +1,5 @@
const cacheVersion = '1.5'; const cacheVersion = '1.5';
const cacheName = `minexlauncher-v${cacheVersion}`; const cacheName = `minexlauncher-full-v${cacheVersion}`;
self.addEventListener('install', (event) => { self.addEventListener('install', (event) => {
event.waitUntil( event.waitUntil(

73
public/sw.js Normal file
View File

@ -0,0 +1,73 @@
const cacheVersion = '1.5';
const cacheName = `minexlauncher-v${cacheVersion}`;
const offlineUrl = '/offline.html';
const cacheAssets = [
offlineUrl,
'/resources/images/icons/favicon.webp',
'/resources/scripts/google-tag.js',
'/resources/scripts/main.js',
'/resources/styles/mobile.css',
'/resources/styles/themes/default.css',
'/resources/styles/themes/light.css',
'/resources/styles/themes/hyperdark.css',
'/resources/styles/themes/overworld.css',
'/resources/styles/themes/nether.css',
'/resources/styles/themes/the-end.css',
'/resources/styles/themes/cherry-blossom.css',
'/resources/styles/themes/retro.css',
'/resources/styles/themes/starfall.css',
'/resources/styles/themes/campfire.css',
'/resources/images/backgrounds/themes/overworld.webp',
'/resources/images/backgrounds/themes/nether.webp',
'/resources/images/backgrounds/themes/the-end.webp',
'/resources/images/backgrounds/themes/cherry-blossom.webp',
'/resources/images/backgrounds/themes/retro.webp',
'/resources/images/backgrounds/themes/starfall.webp',
'/resources/images/backgrounds/themes/campfire.webp',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName).then(async (cache) => {
const totalAssets = cacheAssets.length;
let cachedAssets = 0;
for (const asset of cacheAssets) {
await cache.add(asset);
const progress = `${++cachedAssets}/${totalAssets}`;
console.log(`Cached: ${asset} (${progress})`);
}
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).catch(() => {
return caches.match(offlineUrl);
})
);
} else {
event.respondWith(
fetch(event.request).catch(() => {
return caches.match(event.request);
})
);
}
});

View File

@ -266,10 +266,10 @@ const detect = {
}; };
const serviceworker = { const serviceworker = {
register: function () { register: function (url: string) {
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js').then(() => { navigator.serviceWorker.register(url).then(() => {
navigator.serviceWorker.addEventListener('message', (event) => { navigator.serviceWorker.addEventListener('message', (event) => {
if (event.origin === window.location.origin) { if (event.origin === window.location.origin) {
if (event.data.title === 'sw-install-complete') { if (event.data.title === 'sw-install-complete') {
@ -281,23 +281,6 @@ const serviceworker = {
}); });
} }
}, },
unregister: function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then((registrations) => {
for (const registration of registrations) {
registration.unregister().then(() => {
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
return caches.delete(key);
})
);
});
});
}
});
}
},
}; };
if (window.location.pathname === '/') { if (window.location.pathname === '/') {
@ -310,9 +293,9 @@ if (window.location.pathname === '/') {
}); });
if (storage.local.get('offlineCache') === true) { if (storage.local.get('offlineCache') === true) {
serviceworker.register(); serviceworker.register('/sw-full.js');
} else { } else {
serviceworker.unregister(); serviceworker.register('/sw.js');
} }
} else { } else {
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {

View File

@ -28,10 +28,10 @@ if (window.location.pathname === '/settings/') {
offlineCheckbox.addEventListener('change', () => { offlineCheckbox.addEventListener('change', () => {
storage.local.set('offlineCache', offlineCheckbox.checked); storage.local.set('offlineCache', offlineCheckbox.checked);
if (offlineCheckbox.checked) { if (offlineCheckbox.checked) {
serviceworker.register(); serviceworker.register('/sw-full.js');
alert('Offline cache is now downloading.\nThe download size is about 1GB, so it may take a while.'); alert('Offline cache is now downloading.\nThe download size is about 1GB, so it may take a while.');
} else { } else {
serviceworker.unregister(); serviceworker.register('/sw.js');
alert('Offline cache has been deleted.'); alert('Offline cache has been deleted.');
} }
}); });
@ -74,14 +74,16 @@ if (window.location.pathname === '/welcome.html') {
storage.local.set('lastVersion', launcherVersion); storage.local.set('lastVersion', launcherVersion);
if (offlineCheckbox.checked) { if (offlineCheckbox.checked) {
serviceworker.register(); serviceworker.register('/sw-full.js');
alert('Offline cache is now downloading.\nThe download size is about 1GB, so it may take a while.'); alert('Offline cache is now downloading.\nThe download size is about 1GB, so it may take a while.');
try { try {
// @ts-expect-error // @ts-expect-error
installPwaEvent.prompt(); installPwaEvent.prompt();
} catch (error) { } catch (error) {
console.log('Failed to prompt PWA install:', error); console.error('Failed to prompt PWA install:', error);
} }
} else {
serviceworker.register('/sw.js');
} }
// @ts-expect-error // @ts-expect-error