style(pwa): use 2 spaces indentation for sw.js and swcache.js

This commit is contained in:
Cotes Chung 2023-09-10 21:45:27 +08:00
parent 52084f85d4
commit 1a041e0443
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,6 +1,5 @@
--- ---
layout: compress layout: compress
# The list to be cached by PWA # The list to be cached by PWA
--- ---

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 => {
}) })
); );
}); });