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,49 +1,48 @@
--- ---
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/style.css" | relative_url }}',
/* --- PWA --- */ /* --- PWA --- */
'{{ "/app.js" | relative_url }}', '{{ "/app.js" | relative_url }}',
'{{ "/sw.js" | relative_url }}', '{{ "/sw.js" | relative_url }}',
/* --- HTML --- */ /* --- HTML --- */
'{{ "/index.html" | relative_url }}', '{{ "/index.html" | relative_url }}',
'{{ "/404.html" | relative_url }}', '{{ "/404.html" | relative_url }}',
{% for tab in site.tabs %} {% for tab in site.tabs %}
'{{ tab.url | relative_url }}', '{{ tab.url | relative_url }}',
{% endfor %} {% endfor %}
/* --- Favicons & compressed JS --- */ /* --- Favicons & compressed JS --- */
{% assign cache_list = site.static_files | where: 'swcache', true %} {% assign cache_list = site.static_files | where: 'swcache', true %}
{% for file in cache_list %} {% for file in cache_list %}
'{{ file.path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} '{{ file.path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%}
{% endfor %} {% endfor %}
]; ];
/* The request url with below domain will be cached */ /* The request url with below domain will be cached */
const allowedDomains = [ const allowedDomains = [
{% if site.google_analytics.id != empty and site.google_analytics.id %} {% if site.google_analytics.id != empty and site.google_analytics.id %}
'www.googletagmanager.com', 'www.googletagmanager.com',
'www.google-analytics.com', 'www.google-analytics.com',
{% endif %} {% endif %}
'{{ site.url | split: "//" | last }}', '{{ site.url | split: "//" | last }}',
{% if site.img_cdn contains '//' and site.img_cdn %} {% if site.img_cdn contains '//' and site.img_cdn %}
'{{ site.img_cdn | split: '//' | last | split: '/' | first }}', '{{ site.img_cdn | split: '//' | last | split: '/' | first }}',
{% endif %} {% endif %}
'fonts.gstatic.com', 'fonts.gstatic.com',
'fonts.googleapis.com', 'fonts.googleapis.com',
'cdn.jsdelivr.net', 'cdn.jsdelivr.net',
'polyfill.io' 'polyfill.io'
]; ];
/* Requests that include the following path will be banned */ /* Requests that include the following path will be banned */

View file

@ -9,82 +9,81 @@ self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}');
const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M%S" }}'; const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M%S" }}';
function verifyDomain(url) { function verifyDomain(url) {
for (const domain of allowedDomains) { for (const domain of allowedDomains) {
const regex = RegExp(`^http(s)?:\/\/${domain}\/`); const regex = RegExp(`^http(s)?:\/\/${domain}\/`);
if (regex.test(url)) { if (regex.test(url)) {
return true; return true;
}
} }
}
return false; return false;
} }
function isExcluded(url) { function isExcluded(url) {
for (const item of denyUrls) { for (const item of denyUrls) {
if (url === item) { if (url === item) {
return true; return true;
}
} }
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);
} }
})
);
}) })
); );
})
);
}); });
self.addEventListener('message', (event) => { self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') { if (event.data === 'SKIP_WAITING') {
self.skipWaiting(); self.skipWaiting();
} }
}); });
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 (
!verifyDomain(url) || event.request.method !== 'GET' ||
isExcluded(url)) { !verifyDomain(url) ||
return response; 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();
*/
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);
}); });
return response; return response;
}); });
}) })
); );
}); });