Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move serviceworker to workbox and fix SSE interference #11538

Merged
merged 13 commits into from
May 22, 2020
Prev Previous commit
Next Next commit
add version-based cache invalidation
  • Loading branch information
silverwind committed May 22, 2020
commit 5113a3db42ce895d7cc7d63a8107eb087ec0fda1
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ rules:
no-cond-assign: [2, except-parens]
no-console: [1, {allow: [info, warn, error]}]
no-continue: [0]
no-empty: [2, {allowEmptyCatch: true}]
no-eq-null: [2]
no-mixed-operators: [0]
no-multi-assign: [0]
Expand Down
1 change: 1 addition & 0 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
</script>
<script>
window.config = {
AppVer: '{{AppVer}}',
AppSubUrl: '{{AppSubUrl}}',
StaticUrlPrefix: '{{StaticUrlPrefix}}',
UseServiceWorker: {{UseServiceWorker}},
Expand Down
17 changes: 16 additions & 1 deletion web_src/js/features/serviceworker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {UseServiceWorker, AppSubUrl} = window.config;
const {UseServiceWorker, AppSubUrl, AppVer} = window.config;
const cacheName = 'static-cache-v2';

async function unregister() {
for (const registration of await navigator.serviceWorker.getRegistrations()) {
Expand All @@ -11,9 +12,23 @@ async function unregister() {
export default async function initServiceWorker() {
if (!('serviceWorker' in navigator)) return;

const cacheKey = AppVer;
const storedCacheKey = localStorage.getItem('serviceWorkerCacheKey');

// invalidate cache if it belongs to a different gitea version
if (cacheKey && storedCacheKey !== cacheKey) {
try {
await caches.delete(cacheName);
} catch (_err) {}
}

// register or unregister the service worker script
if (UseServiceWorker) {
try {
await navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`);
if (cacheKey) {
localStorage.setItem('serviceWorkerCacheKey', cacheKey);
}
} catch (err) {
console.error(err);
await unregister();
Expand Down
4 changes: 3 additions & 1 deletion web_src/js/serviceworker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {registerRoute} from 'workbox-routing';
import {StaleWhileRevalidate} from 'workbox-strategies';

const cacheName = 'static-cache-v2';

const cachedDestinations = new Set([
'manifest',
'script',
Expand All @@ -10,5 +12,5 @@ const cachedDestinations = new Set([

registerRoute(
({request}) => cachedDestinations.has(request.destination),
new StaleWhileRevalidate({cacheName: 'static-cache-v2'}),
new StaleWhileRevalidate({cacheName}),
);