Merge branch 'hotfix/6.2.1'

This commit is contained in:
Cotes Chung 2023-09-11 00:36:54 +08:00
commit f949bda15b
No known key found for this signature in database
GPG key ID: 0D9E54843167A808
2 changed files with 84 additions and 86 deletions

View file

@ -1,12 +1,11 @@
--- ---
layout: compress layout: compress
# The list to be cached by PWA # The list to be cached by PWA
--- ---
const resource = [ const resource = [
/* --- CSS --- */ /* --- CSS --- */
'{{ "/assets/css/style.css" | relative_url }}', '{{ "/assets/css/:THEME.css" | replace: ':THEME', site.theme | relative_url }}',
/* --- PWA --- */ /* --- PWA --- */
'{{ "/app.js" | relative_url }}', '{{ "/app.js" | relative_url }}',

View file

@ -28,19 +28,19 @@ function isExcluded(url) {
return false; return false;
} }
self.addEventListener('install', event => { self.addEventListener('install', (event) => {
event.waitUntil( event.waitUntil(
caches.open(cacheName).then(cache => { caches.open(cacheName).then((cache) => {
return cache.addAll(resource); return cache.addAll(resource);
}) })
); );
}); });
self.addEventListener('activate', event => { self.addEventListener('activate', (event) => {
event.waitUntil( event.waitUntil(
caches.keys().then(keyList => { caches.keys().then((keyList) => {
return Promise.all( return Promise.all(
keyList.map(key => { keyList.map((key) => {
if (key !== cacheName) { if (key !== cacheName) {
return caches.delete(key); return caches.delete(key);
} }
@ -56,28 +56,28 @@ self.addEventListener('message', (event) => {
} }
}); });
self.addEventListener('fetch', event => { self.addEventListener('fetch', (event) => {
event.respondWith( event.respondWith(
caches.match(event.request).then(response => { caches.match(event.request).then((response) => {
if (response) { if (response) {
return response; return response;
} }
return fetch(event.request).then(response => { return fetch(event.request).then((response) => {
const url = event.request.url; const url = event.request.url;
if (event.request.method !== 'GET' || if (
event.request.method !== 'GET' ||
!verifyDomain(url) || !verifyDomain(url) ||
isExcluded(url)) { isExcluded(url)
) {
return response; return response;
} }
/* /* see: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests> */
see: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests>
*/
let responseToCache = response.clone(); let responseToCache = response.clone();
caches.open(cacheName).then(cache => { caches.open(cacheName).then((cache) => {
/* console.log('[sw] Caching new resource: ' + event.request.url); */ /* console.log('[sw] Caching new resource: ' + event.request.url); */
cache.put(event.request, responseToCache); cache.put(event.request, responseToCache);
}); });
@ -87,4 +87,3 @@ self.addEventListener('fetch', event => {
}) })
); );
}); });