Merge branch 'hotfix/6.2.1' into production

This commit is contained in:
Cotes Chung 2023-09-11 00:36:50 +08:00
commit 14d3960ca0
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
# The list to be cached by PWA
---
const resource = [
/* --- CSS --- */
'{{ "/assets/css/style.css" | relative_url }}',
'{{ "/assets/css/:THEME.css" | replace: ':THEME', site.theme | relative_url }}',
/* --- PWA --- */
'{{ "/app.js" | relative_url }}',

View file

@ -28,19 +28,19 @@ function isExcluded(url) {
return false;
}
self.addEventListener('install', event => {
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(cacheName).then(cache => {
caches.open(cacheName).then((cache) => {
return cache.addAll(resource);
})
);
});
self.addEventListener('activate', event => {
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(keyList => {
caches.keys().then((keyList) => {
return Promise.all(
keyList.map(key => {
keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
@ -56,28 +56,28 @@ self.addEventListener('message', (event) => {
}
});
self.addEventListener('fetch', event => {
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then(response => {
caches.match(event.request).then((response) => {
if (response) {
return response;
}
return fetch(event.request).then(response => {
return fetch(event.request).then((response) => {
const url = event.request.url;
if (event.request.method !== 'GET' ||
if (
event.request.method !== 'GET' ||
!verifyDomain(url) ||
isExcluded(url)) {
isExcluded(url)
) {
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();
caches.open(cacheName).then(cache => {
caches.open(cacheName).then((cache) => {
/* console.log('[sw] Caching new resource: ' + event.request.url); */
cache.put(event.request, responseToCache);
});
@ -87,4 +87,3 @@ self.addEventListener('fetch', event => {
})
);
});