Skip to content

Commit 323e769

Browse files
committed
Remove the ServiceWorker and manifest.json
There is barely any performance benefit of SW-based caching and it causes a lot of issues after updating a instance and when the cache is stale, causing weird UI issues like chunk loading errors and other unexpected behaviour. I kept the unregistration in the code so existing serviceworkers will properly uninstall after an update. We may be able to eventually drop the code completely in a future version after most users have ran the uninstallation. manifest.json is removed because it serves no real purpose without a serviceworker and to eliminate another HTTP request for the sake of performance. Fixes: #12344
1 parent faa676c commit 323e769

File tree

12 files changed

+10
-105
lines changed

12 files changed

+10
-105
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ FOMANTIC_DEST_DIR := web_src/fomantic/build
9696
WEBPACK_SOURCES := $(shell find web_src/js web_src/less -type f) $(FOMANTIC_DEST)
9797
WEBPACK_CONFIGS := webpack.config.js
9898
WEBPACK_DEST := public/js/index.js public/css/index.css
99-
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack public/serviceworker.js
99+
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack
100100

101101
BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go
102102
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))

custom/conf/app.example.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ REACTIONS = +1, -1, laugh, hooray, confused, heart, rocket, eyes
190190
DEFAULT_SHOW_FULL_NAME = false
191191
; Whether to search within description at repository search on explore page.
192192
SEARCH_REPO_DESCRIPTION = true
193-
; Whether to enable a Service Worker to cache frontend assets
194-
USE_SERVICE_WORKER = true
195193

196194
[ui.admin]
197195
; Number of users that are displayed on one page

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
137137
For custom reactions, add a tightly cropped square image to public/emoji/img/reaction_name.png
138138
- `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.
139139
- `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page.
140-
- `USE_SERVICE_WORKER`: **true**: Whether to enable a Service Worker to cache frontend assets.
141140

142141
### UI - Admin (`ui.admin`)
143142

docs/content/doc/advanced/hacking-on-gitea.en-us.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ Before committing, make sure the linters pass:
151151
make lint-frontend
152152
```
153153

154-
Note: When working on frontend code, set `USE_SERVICE_WORKER` to `false` in `app.ini` to prevent undesirable caching of frontend assets.
155-
156154
### Building and adding SVGs
157155

158156
SVG icons are built using the `make svg` target which compiles the icon sources defined in `build/generate-svg.js` into the output directory `public/img/svg`. Custom icons can be added in the `web_src/svg` directory.

modules/setting/setting.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ var (
179179
Reactions []string
180180
ReactionsMap map[string]bool
181181
SearchRepoDescription bool
182-
UseServiceWorker bool
183182

184183
Notification struct {
185184
MinTimeout time.Duration
@@ -1012,7 +1011,6 @@ func NewContext() {
10121011
UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true)
10131012
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
10141013
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
1015-
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(true)
10161014

10171015
HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt"))
10181016

modules/templates/helper.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ func NewFuncMap() []template.FuncMap {
169169
"MetaKeywords": func() string {
170170
return setting.UI.Meta.Keywords
171171
},
172-
"UseServiceWorker": func() bool {
173-
return setting.UI.UseServiceWorker
174-
},
175172
"FilenameIsImage": func(filename string) bool {
176173
mimeType := mime.TypeByExtension(filepath.Ext(filename))
177174
return strings.HasPrefix(mimeType, "image/")

package-lock.json

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
"webpack": "4.44.0",
4646
"webpack-cli": "3.3.12",
4747
"webpack-fix-style-only-entries": "0.5.1",
48-
"workbox-routing": "5.1.3",
49-
"workbox-strategies": "5.1.3",
5048
"worker-loader": "2.0.0",
5149
"wrap-ansi": "7.0.0"
5250
},

templates/base/head.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
AppVer: '{{AppVer}}',
3131
AppSubUrl: '{{AppSubUrl}}',
3232
StaticUrlPrefix: '{{StaticUrlPrefix}}',
33-
UseServiceWorker: {{UseServiceWorker}},
3433
csrf: '{{.CsrfToken}}',
3534
HighlightJS: {{if .RequireHighlightJS}}true{{else}}false{{end}},
3635
SimpleMDE: {{if .RequireSimpleMDE}}true{{else}}false{{end}},

web_src/js/features/serviceworker.js

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,23 @@
1-
const {UseServiceWorker, AppSubUrl, AppVer} = window.config;
2-
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script
3-
41
async function unregister() {
2+
if (!('serviceWorker' in navigator)) return;
53
const registrations = await navigator.serviceWorker.getRegistrations();
64
await Promise.all(registrations.map((registration) => {
75
return registration.active && registration.unregister();
86
}));
97
}
108

119
async function invalidateCache() {
10+
if (!caches || !caches.keys) return;
1211
const cacheKeys = await caches.keys();
1312
await Promise.all(cacheKeys.map((key) => {
14-
return key.startsWith(cachePrefix) && caches.delete(key);
13+
return key.startsWith('static-cache-v') && caches.delete(key);
1514
}));
1615
}
1716

18-
async function checkCacheValidity() {
19-
const cacheKey = AppVer;
20-
const storedCacheKey = localStorage.getItem('staticCacheKey');
21-
22-
// invalidate cache if it belongs to a different gitea version
23-
if (cacheKey && storedCacheKey !== cacheKey) {
24-
await invalidateCache();
25-
localStorage.setItem('staticCacheKey', cacheKey);
26-
}
27-
}
28-
2917
export default async function initServiceWorker() {
30-
if (!('serviceWorker' in navigator)) return;
31-
32-
if (UseServiceWorker) {
33-
try {
34-
// normally we'd serve the service worker as a static asset from StaticUrlPrefix but
35-
// the spec strictly requires it to be same-origin so it has to be AppSubUrl to work
36-
await Promise.all([
37-
checkCacheValidity(),
38-
navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`),
39-
]);
40-
} catch (err) {
41-
console.error(err);
42-
await Promise.all([
43-
invalidateCache(),
44-
unregister(),
45-
]);
46-
}
47-
} else {
48-
await Promise.all([
49-
invalidateCache(),
50-
unregister(),
51-
]);
52-
}
18+
// we once had a service worker, if it's present, remove it and wipe its cache
19+
await Promise.all([
20+
invalidateCache(),
21+
unregister(),
22+
]);
5323
}

web_src/js/serviceworker.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

webpack.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ module.exports = {
5050
swagger: [
5151
resolve(__dirname, 'web_src/js/standalone/swagger.js'),
5252
],
53-
serviceworker: [
54-
resolve(__dirname, 'web_src/js/serviceworker.js'),
55-
],
5653
'eventsource.sharedworker': [
5754
resolve(__dirname, 'web_src/js/features/eventsource.sharedworker.js'),
5855
],
@@ -61,11 +58,7 @@ module.exports = {
6158
devtool: false,
6259
output: {
6360
path: resolve(__dirname, 'public'),
64-
filename: ({chunk}) => {
65-
// serviceworker can only manage assets below it's script's directory so
66-
// we have to put it in / instead of /js/
67-
return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js';
68-
},
61+
filename: 'js/[name].js',
6962
chunkFilename: 'js/[name].js',
7063
},
7164
optimization: {

0 commit comments

Comments
 (0)