diff options
author | silverwind <me@silverwind.io> | 2023-05-31 04:07:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-31 02:07:04 +0000 |
commit | 50bd7d0b24016b0cf48dfbafe84b5953fe20c34f (patch) | |
tree | af5df7c26729ada5dbf27ba9d6b970bac35b49bc /web_src | |
parent | 28a89e360f720c5ca7fb71319beb2519adad5972 (diff) | |
download | gitea-50bd7d0b24016b0cf48dfbafe84b5953fe20c34f.tar.gz gitea-50bd7d0b24016b0cf48dfbafe84b5953fe20c34f.zip |
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.
## :warning: BREAKING
You can remove the config `[ui].USE_SERVICE_WORKER` from your `app.ini`
now.
Diffstat (limited to 'web_src')
-rw-r--r-- | web_src/js/features/serviceworker.js | 57 | ||||
-rw-r--r-- | web_src/js/index.js | 2 | ||||
-rw-r--r-- | web_src/js/serviceworker.js | 23 |
3 files changed, 0 insertions, 82 deletions
diff --git a/web_src/js/features/serviceworker.js b/web_src/js/features/serviceworker.js deleted file mode 100644 index 32d2e04cd6..0000000000 --- a/web_src/js/features/serviceworker.js +++ /dev/null @@ -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(); - } -} diff --git a/web_src/js/index.js b/web_src/js/index.js index 8f4d379893..3d8a7fc325 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -11,7 +11,6 @@ import {initHeatmap} from './features/heatmap.js'; import {initImageDiff} from './features/imagediff.js'; import {initRepoMigration} from './features/repo-migration.js'; import {initRepoProject} from './features/repo-projects.js'; -import {initServiceWorker} from './features/serviceworker.js'; import {initTableSort} from './features/tablesort.js'; import {initAdminUserListSearchForm} from './features/admin/users.js'; import {initAdminConfigs} from './features/admin/config.js'; @@ -116,7 +115,6 @@ onDomReady(() => { initImageDiff(); initMarkupAnchors(); initMarkupContent(); - initServiceWorker(); initSshKeyFormParser(); initStopwatch(); initTableSort(); diff --git a/web_src/js/serviceworker.js b/web_src/js/serviceworker.js deleted file mode 100644 index c96ef8bd97..0000000000 --- a/web_src/js/serviceworker.js +++ /dev/null @@ -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}), -); |