mirror of https://github.com/go-gitea/gitea
Remove the service worker (#25010)
It's been disabled by default since 1.17 (https://github.com/go-gitea/gitea/pull/18914), and it never really delivered any benefit except being another cache layer that has its own unsolved invalidation issues. HTTP cache works, we don't need two cache layers at the browser for assets. ## ⚠️ BREAKING You can remove the config `[ui].USE_SERVICE_WORKER` from your `app.ini` now.pull/25012/head
parent
28a89e360f
commit
50bd7d0b24
@ -1,57 +0,0 @@ |
||||
import {joinPaths, parseUrl} from '../utils.js'; |
||||
|
||||
const {useServiceWorker, assetUrlPrefix, assetVersionEncoded} = window.config; |
||||
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script
|
||||
const workerUrl = `${joinPaths(assetUrlPrefix, 'serviceworker.js')}?v=${assetVersionEncoded}`; |
||||
|
||||
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()) { |
||||
const scriptPath = parseUrl(registration.active?.scriptURL || '').pathname; |
||||
const workerPath = parseUrl(workerUrl).pathname; |
||||
if (scriptPath !== workerPath) await registration.unregister(); |
||||
} |
||||
} |
||||
|
||||
async function invalidateCache() { |
||||
for (const key of await caches.keys()) { |
||||
if (key.startsWith(cachePrefix)) caches.delete(key); |
||||
} |
||||
} |
||||
|
||||
async function checkCacheValidity() { |
||||
const cacheKey = assetVersionEncoded; |
||||
const storedCacheKey = localStorage.getItem('staticCacheKey'); |
||||
|
||||
// invalidate cache if it belongs to a different gitea version
|
||||
if (cacheKey && storedCacheKey !== cacheKey) { |
||||
await invalidateCache(); |
||||
localStorage.setItem('staticCacheKey', cacheKey); |
||||
} |
||||
} |
||||
|
||||
export async function initServiceWorker() { |
||||
if (!('serviceWorker' in navigator)) return; |
||||
|
||||
if (useServiceWorker) { |
||||
// unregister all service workers where scriptURL does not match the current one
|
||||
await unregisterOtherWorkers(); |
||||
try { |
||||
// the spec strictly requires it to be same-origin so the AssetUrlPrefix should contain AppSubUrl
|
||||
await checkCacheValidity(); |
||||
await navigator.serviceWorker.register(workerUrl); |
||||
} catch (err) { |
||||
console.error(err); |
||||
await invalidateCache(); |
||||
await unregisterAll(); |
||||
} |
||||
} else { |
||||
await invalidateCache(); |
||||
await unregisterAll(); |
||||
} |
||||
} |
@ -1,23 +0,0 @@ |
||||
import {registerRoute} from 'workbox-routing'; |
||||
import {StaleWhileRevalidate} from 'workbox-strategies'; |
||||
|
||||
const cacheName = 'static-cache-v2'; |
||||
|
||||
// disable workbox debug logging in development, remove when debugging the service worker
|
||||
self.__WB_DISABLE_DEV_LOGS = true; |
||||
|
||||
// see https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination for possible values
|
||||
const cachedDestinations = new Set([ |
||||
'font', |
||||
'manifest', |
||||
'paintworklet', |
||||
'script', |
||||
'sharedworker', |
||||
'style', |
||||
'worker', |
||||
]); |
||||
|
||||
registerRoute( |
||||
({request}) => cachedDestinations.has(request.destination), |
||||
new StaleWhileRevalidate({cacheName}), |
||||
); |
Loading…
Reference in new issue