From 50bd7d0b24016b0cf48dfbafe84b5953fe20c34f Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 31 May 2023 04:07:04 +0200 Subject: [PATCH] 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. --- .dockerignore | 1 - .gitignore | 1 - .ignore | 1 + Makefile | 2 +- custom/conf/app.example.ini | 3 - .../config-cheat-sheet.en-us.md | 1 - modules/setting/ui.go | 2 - modules/templates/helper.go | 3 - package-lock.json | 23 -------- package.json | 2 - templates/base/head_script.tmpl | 1 - web_src/js/features/serviceworker.js | 57 ------------------- web_src/js/index.js | 2 - web_src/js/serviceworker.js | 23 -------- webpack.config.js | 9 +-- 15 files changed, 3 insertions(+), 128 deletions(-) delete mode 100644 web_src/js/features/serviceworker.js delete mode 100644 web_src/js/serviceworker.js diff --git a/.dockerignore b/.dockerignore index 1ce2a8761..2a4142088 100644 --- a/.dockerignore +++ b/.dockerignore @@ -76,7 +76,6 @@ cpu.out /yarn-error.log /npm-debug.log* /public/js -/public/serviceworker.js /public/css /public/fonts /public/img/webpack diff --git a/.gitignore b/.gitignore index ad141c30c..7ae3455d4 100644 --- a/.gitignore +++ b/.gitignore @@ -77,7 +77,6 @@ cpu.out /yarn-error.log /npm-debug.log* /public/js -/public/serviceworker.js /public/css /public/fonts /public/img/webpack diff --git a/.ignore b/.ignore index a315e5b5b..cd869d51c 100644 --- a/.ignore +++ b/.ignore @@ -1,5 +1,6 @@ *.min.css *.min.js +/assets/*.json /modules/options/bindata.go /modules/public/bindata.go /modules/templates/bindata.go diff --git a/Makefile b/Makefile index 16841796b..664839273 100644 --- a/Makefile +++ b/Makefile @@ -114,7 +114,7 @@ FOMANTIC_WORK_DIR := web_src/fomantic WEBPACK_SOURCES := $(shell find web_src/js web_src/css -type f) WEBPACK_CONFIGS := webpack.config.js WEBPACK_DEST := public/js/index.js public/css/index.css -WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack public/serviceworker.js +WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST)) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 01abb15c1..23dfefa01 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -1208,9 +1208,6 @@ LEVEL = Info ;; Whether to search within description at repository search on explore page. ;SEARCH_REPO_DESCRIPTION = true ;; -;; Whether to enable a Service Worker to cache frontend assets -;USE_SERVICE_WORKER = false -;; ;; Whether to only show relevant repos on the explore page when no keyword is specified and default sorting is used. ;; A repo is considered irrelevant if it's a fork or if it has no metadata (no description, no icon, no topic). ;ONLY_SHOW_RELEVANT_REPOS = false diff --git a/docs/content/doc/administration/config-cheat-sheet.en-us.md b/docs/content/doc/administration/config-cheat-sheet.en-us.md index 514719a99..e3612f375 100644 --- a/docs/content/doc/administration/config-cheat-sheet.en-us.md +++ b/docs/content/doc/administration/config-cheat-sheet.en-us.md @@ -230,7 +230,6 @@ The following configuration set `Content-Type: application/vnd.android.package-a add it to this config. - `DEFAULT_SHOW_FULL_NAME`: **false**: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used. - `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page. -- `USE_SERVICE_WORKER`: **false**: Whether to enable a Service Worker to cache frontend assets. - `ONLY_SHOW_RELEVANT_REPOS`: **false** Whether to only show relevant repos on the explore page when no keyword is specified and default sorting is used. A repo is considered irrelevant if it's a fork or if it has no metadata (no description, no icon, no topic). diff --git a/modules/setting/ui.go b/modules/setting/ui.go index aa2c64019..41438db40 100644 --- a/modules/setting/ui.go +++ b/modules/setting/ui.go @@ -32,7 +32,6 @@ var UI = struct { CustomEmojis []string CustomEmojisMap map[string]string `ini:"-"` SearchRepoDescription bool - UseServiceWorker bool OnlyShowRelevantRepos bool Notification struct { @@ -136,7 +135,6 @@ func loadUIFrom(rootCfg ConfigProvider) { UI.ShowUserEmail = sec.Key("SHOW_USER_EMAIL").MustBool(true) UI.DefaultShowFullName = sec.Key("DEFAULT_SHOW_FULL_NAME").MustBool(false) UI.SearchRepoDescription = sec.Key("SEARCH_REPO_DESCRIPTION").MustBool(true) - UI.UseServiceWorker = sec.Key("USE_SERVICE_WORKER").MustBool(false) // OnlyShowRelevantRepos=false is important for many private/enterprise instances, // because many private repositories do not have "description/topic", users just want to search by their names. diff --git a/modules/templates/helper.go b/modules/templates/helper.go index cae745230..2f2ef4404 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -127,9 +127,6 @@ func NewFuncMap() template.FuncMap { "MetaKeywords": func() string { return setting.UI.Meta.Keywords }, - "UseServiceWorker": func() bool { - return setting.UI.UseServiceWorker - }, "EnableTimetracking": func() bool { return setting.Service.EnableTimetracking }, diff --git a/package-lock.json b/package-lock.json index 162026703..5e077b8a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,8 +51,6 @@ "vue3-calendar-heatmap": "2.0.5", "webpack": "5.84.1", "webpack-cli": "5.1.1", - "workbox-routing": "6.6.0", - "workbox-strategies": "6.6.0", "wrap-ansi": "8.1.0" }, "devDependencies": { @@ -11045,27 +11043,6 @@ "node": ">=0.10.0" } }, - "node_modules/workbox-core": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/workbox-core/-/workbox-core-6.6.0.tgz", - "integrity": "sha512-GDtFRF7Yg3DD859PMbPAYPeJyg5gJYXuBQAC+wyrWuuXgpfoOrIQIvFRZnQ7+czTIQjIr1DhLEGFzZanAT/3bQ==" - }, - "node_modules/workbox-routing": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/workbox-routing/-/workbox-routing-6.6.0.tgz", - "integrity": "sha512-x8gdN7VDBiLC03izAZRfU+WKUXJnbqt6PG9Uh0XuPRzJPpZGLKce/FkOX95dWHRpOHWLEq8RXzjW0O+POSkKvw==", - "dependencies": { - "workbox-core": "6.6.0" - } - }, - "node_modules/workbox-strategies": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/workbox-strategies/-/workbox-strategies-6.6.0.tgz", - "integrity": "sha512-eC07XGuINAKUWDnZeIPdRdVja4JQtTuc35TZ8SwMb1ztjp7Ddq2CJ4yqLvWzFWGlYI7CG/YGqaETntTxBGdKgQ==", - "dependencies": { - "workbox-core": "6.6.0" - } - }, "node_modules/wrap-ansi": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz", diff --git a/package.json b/package.json index 72563f8bf..73ce86f33 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,6 @@ "vue3-calendar-heatmap": "2.0.5", "webpack": "5.84.1", "webpack-cli": "5.1.1", - "workbox-routing": "6.6.0", - "workbox-strategies": "6.6.0", "wrap-ansi": "8.1.0" }, "devDependencies": { diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index 670d146b5..4e1dffa47 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -12,7 +12,6 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, customEmojis: {{CustomEmojis}}, - useServiceWorker: {{UseServiceWorker}}, csrfToken: '{{.CsrfToken}}', pageData: {{.PageData}}, notificationSettings: {{NotificationSettings}}, {{/*a map provided by NewFuncMap in helper.go*/}} diff --git a/web_src/js/features/serviceworker.js b/web_src/js/features/serviceworker.js deleted file mode 100644 index 32d2e04cd..000000000 --- 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 8f4d37989..3d8a7fc32 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 c96ef8bd9..000000000 --- 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}), -); diff --git a/webpack.config.js b/webpack.config.js index e64f4acb5..ae71a0599 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -62,9 +62,6 @@ export default { fileURLToPath(new URL('web_src/js/standalone/swagger.js', import.meta.url)), fileURLToPath(new URL('web_src/css/standalone/swagger.css', import.meta.url)), ], - serviceworker: [ - fileURLToPath(new URL('web_src/js/serviceworker.js', import.meta.url)), - ], 'eventsource.sharedworker': [ fileURLToPath(new URL('web_src/js/features/eventsource.sharedworker.js', import.meta.url)), ], @@ -73,11 +70,7 @@ export default { devtool: false, output: { path: fileURLToPath(new URL('public', import.meta.url)), - filename: ({chunk}) => { - // serviceworker can only manage assets below it's script's directory so - // we have to put it in / instead of /js/ - return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js'; - }, + filename: () => 'js/[name].js', chunkFilename: ({chunk}) => { const language = (/monaco.*languages?_.+?_(.+?)_/.exec(chunk.id) || [])[1]; return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;