Skip to content

Commit 339f5bb

Browse files
authored
Fix serviceworker output file and misc improvements (#11562) (#11610)
* Fix serviceworker output file and misc improvements - Fix output file location for production build - Cache more asset types: fonts and worker variants - Parallelize a few tasks during initalization - Only invalidate caches starting with our prefix - Remove public/serviceworker.js before building - Remove font preloads, they cause strange cors issues - Misc eslint config adjustments * remove webpack output files on watch-frontend
1 parent 1ae5435 commit 339f5bb

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ globals:
2525
Tribute: false
2626

2727
overrides:
28-
- files: ["web_src/**/*.worker.js"]
28+
- files: ["web_src/**/*.worker.js", "web_src/js/serviceworker.js"]
2929
env:
3030
worker: true
3131
rules:
@@ -58,6 +58,7 @@ rules:
5858
no-restricted-syntax: [0]
5959
no-return-await: [0]
6060
no-shadow: [0]
61+
no-underscore-dangle: [0]
6162
no-unused-vars: [2, {args: all, argsIgnorePattern: ^_, varsIgnorePattern: ^_, ignoreRestSiblings: true}]
6263
no-use-before-define: [0]
6364
no-var: [2]

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ GO_PACKAGES ?= $(filter-out code.gitea.io/gitea/integrations/migration-test,$(fi
8888
WEBPACK_SOURCES := $(shell find web_src/js web_src/less -type f)
8989
WEBPACK_CONFIGS := webpack.config.js
9090
WEBPACK_DEST := public/js/index.js public/css/index.css
91-
WEBPACK_DEST_DIRS := public/js public/css public/fonts
91+
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/serviceworker.js
9292

9393
BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go
9494
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))
@@ -194,7 +194,7 @@ node-check:
194194

195195
.PHONY: clean-all
196196
clean-all: clean
197-
rm -rf $(WEBPACK_DEST_DIRS) $(FOMANTIC_DEST_DIR)
197+
rm -rf $(WEBPACK_DEST_ENTRIES) $(FOMANTIC_DEST_DIR)
198198

199199
.PHONY: clean
200200
clean:
@@ -295,6 +295,7 @@ lint-frontend: node_modules
295295

296296
.PHONY: watch-frontend
297297
watch-frontend: node_modules
298+
rm -rf $(WEBPACK_DEST_ENTRIES)
298299
NODE_ENV=development npx webpack --hide-modules --display-entrypoints=false --watch --progress
299300

300301
.PHONY: test
@@ -598,7 +599,7 @@ $(FOMANTIC_DEST): $(FOMANTIC_CONFIGS) package-lock.json | node_modules
598599
webpack: $(WEBPACK_DEST)
599600

600601
$(WEBPACK_DEST): $(WEBPACK_SOURCES) $(WEBPACK_CONFIGS) package-lock.json | node_modules
601-
rm -rf $(WEBPACK_DEST_DIRS)
602+
rm -rf $(WEBPACK_DEST_ENTRIES)
602603
npx webpack --hide-modules --display-entrypoints=false
603604
@touch $(WEBPACK_DEST)
604605

templates/base/head.tmpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@
9292
<link rel="mask-icon" href="{{StaticUrlPrefix}}/img/gitea-safari.svg" color="#609926">
9393
<link rel="fluid-icon" href="{{StaticUrlPrefix}}/img/gitea-lg.png" title="{{AppName}}">
9494
<link rel="stylesheet" href="{{StaticUrlPrefix}}/vendor/assets/font-awesome/css/font-awesome.min.css">
95-
<link rel="preload" as="font" href="{{StaticUrlPrefix}}/fomantic/themes/default/assets/fonts/icons.woff2" type="font/woff2" crossorigin="anonymous">
96-
<link rel="preload" as="font" href="{{StaticUrlPrefix}}/fomantic/themes/default/assets/fonts/outline-icons.woff2" type="font/woff2" crossorigin="anonymous">
9795
{{if .RequireSimpleMDE}}
9896
<link rel="stylesheet" href="{{StaticUrlPrefix}}/vendor/plugins/simplemde/simplemde.min.css">
9997
{{end}}

web_src/js/features/serviceworker.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const {UseServiceWorker, AppSubUrl, AppVer} = window.config;
2-
const cacheName = 'static-cache-v2';
2+
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script
33

44
async function unregister() {
5-
for (const registration of await navigator.serviceWorker.getRegistrations()) {
6-
const serviceWorker = registration.active;
7-
if (!serviceWorker) continue;
8-
registration.unregister();
9-
}
5+
const registrations = await navigator.serviceWorker.getRegistrations();
6+
await Promise.all(registrations.map((registration) => {
7+
return registration.active && registration.unregister();
8+
}));
109
}
1110

1211
async function invalidateCache() {
13-
await caches.delete(cacheName);
12+
const cacheKeys = await caches.keys();
13+
await Promise.all(cacheKeys.map((key) => {
14+
return key.startsWith(cachePrefix) && caches.delete(key);
15+
}));
1416
}
1517

1618
async function checkCacheValidity() {
@@ -19,7 +21,7 @@ async function checkCacheValidity() {
1921

2022
// invalidate cache if it belongs to a different gitea version
2123
if (cacheKey && storedCacheKey !== cacheKey) {
22-
invalidateCache();
24+
await invalidateCache();
2325
localStorage.setItem('staticCacheKey', cacheKey);
2426
}
2527
}
@@ -28,16 +30,24 @@ export default async function initServiceWorker() {
2830
if (!('serviceWorker' in navigator)) return;
2931

3032
if (UseServiceWorker) {
31-
await checkCacheValidity();
3233
try {
33-
await navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`);
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+
]);
3440
} catch (err) {
3541
console.error(err);
36-
await invalidateCache();
37-
await unregister();
42+
await Promise.all([
43+
invalidateCache(),
44+
unregister(),
45+
]);
3846
}
3947
} else {
40-
await invalidateCache();
41-
await unregister();
48+
await Promise.all([
49+
invalidateCache(),
50+
unregister(),
51+
]);
4252
}
4353
}

web_src/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,7 +2474,7 @@ $(document).ready(async () => {
24742474
}
24752475
});
24762476

2477-
// parallel init of lazy-loaded features
2477+
// parallel init of async loaded features
24782478
await Promise.all([
24792479
highlight(document.querySelectorAll('pre code')),
24802480
initGitGraph(),

web_src/js/serviceworker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import {StaleWhileRevalidate} from 'workbox-strategies';
33

44
const cacheName = 'static-cache-v2';
55

6+
// disable workbox debug logging in development, remove when debugging the service worker
7+
self.__WB_DISABLE_DEV_LOGS = true;
8+
9+
// see https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination for possible values
610
const cachedDestinations = new Set([
11+
'font',
712
'manifest',
13+
'paintworklet',
814
'script',
15+
'sharedworker',
916
'style',
1017
'worker',
1118
]);

0 commit comments

Comments
 (0)