2022-08-23 12:58:04 +00:00
|
|
|
import {joinPaths, parseUrl} from '../utils.js';
|
2021-05-12 18:36:53 +00:00
|
|
|
|
2022-08-23 12:58:04 +00:00
|
|
|
const {useServiceWorker, assetUrlPrefix, appVer, assetVersionEncoded} = window.config;
|
2020-05-24 07:36:40 +00:00
|
|
|
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script
|
2022-08-23 12:58:04 +00:00
|
|
|
const workerUrl = `${joinPaths(assetUrlPrefix, 'serviceworker.js')}?v=${assetVersionEncoded}`;
|
2020-05-22 01:45:34 +00:00
|
|
|
|
2021-05-12 18:36:53 +00:00
|
|
|
async function unregisterAll() {
|
|
|
|
for (const registration of await navigator.serviceWorker.getRegistrations()) {
|
|
|
|
if (registration.active) await registration.unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unregisterOtherWorkers() {
|
|
|
|
for (const registration of await navigator.serviceWorker.getRegistrations()) {
|
2022-08-23 12:58:04 +00:00
|
|
|
const scriptPath = parseUrl(registration.active?.scriptURL || '').pathname;
|
|
|
|
const workerPath = parseUrl(workerUrl).pathname;
|
|
|
|
if (scriptPath !== workerPath) await registration.unregister();
|
2021-05-12 18:36:53 +00:00
|
|
|
}
|
2020-05-22 01:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function invalidateCache() {
|
2021-05-12 18:36:53 +00:00
|
|
|
for (const key of await caches.keys()) {
|
|
|
|
if (key.startsWith(cachePrefix)) caches.delete(key);
|
|
|
|
}
|
2020-05-22 01:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function checkCacheValidity() {
|
2021-10-21 07:37:43 +00:00
|
|
|
const cacheKey = appVer;
|
2020-05-22 01:45:34 +00:00
|
|
|
const storedCacheKey = localStorage.getItem('staticCacheKey');
|
|
|
|
|
|
|
|
// invalidate cache if it belongs to a different gitea version
|
|
|
|
if (cacheKey && storedCacheKey !== cacheKey) {
|
2020-05-24 07:36:40 +00:00
|
|
|
await invalidateCache();
|
2020-05-22 01:45:34 +00:00
|
|
|
localStorage.setItem('staticCacheKey', cacheKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function initServiceWorker() {
|
|
|
|
if (!('serviceWorker' in navigator)) return;
|
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
if (useServiceWorker) {
|
2021-05-12 18:36:53 +00:00
|
|
|
// unregister all service workers where scriptURL does not match the current one
|
|
|
|
await unregisterOtherWorkers();
|
2020-05-22 01:45:34 +00:00
|
|
|
try {
|
2021-06-08 17:46:13 +00:00
|
|
|
// the spec strictly requires it to be same-origin so the AssetUrlPrefix should contain AppSubUrl
|
2021-05-12 18:36:53 +00:00
|
|
|
await checkCacheValidity();
|
2022-08-23 12:58:04 +00:00
|
|
|
await navigator.serviceWorker.register(workerUrl);
|
2020-05-22 01:45:34 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-05-12 18:36:53 +00:00
|
|
|
await invalidateCache();
|
|
|
|
await unregisterAll();
|
2020-05-22 01:45:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-12 18:36:53 +00:00
|
|
|
await invalidateCache();
|
|
|
|
await unregisterAll();
|
2020-05-22 01:45:34 +00:00
|
|
|
}
|
|
|
|
}
|