From 1a041e0443696e6ed22d905c65ba1c88c302aa5f Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Sun, 10 Sep 2023 21:45:27 +0800 Subject: [PATCH] style(pwa): use 2 spaces indentation for `sw.js` and `swcache.js` --- assets/js/data/swcache.js | 57 ++++++++++--------- assets/js/pwa/sw.js | 113 +++++++++++++++++++------------------- 2 files changed, 84 insertions(+), 86 deletions(-) diff --git a/assets/js/data/swcache.js b/assets/js/data/swcache.js index 9ff3899..f4dcc87 100644 --- a/assets/js/data/swcache.js +++ b/assets/js/data/swcache.js @@ -1,49 +1,48 @@ --- layout: compress - # The list to be cached by PWA --- const resource = [ - /* --- CSS --- */ - '{{ "/assets/css/style.css" | relative_url }}', + /* --- CSS --- */ + '{{ "/assets/css/style.css" | relative_url }}', - /* --- PWA --- */ - '{{ "/app.js" | relative_url }}', - '{{ "/sw.js" | relative_url }}', + /* --- PWA --- */ + '{{ "/app.js" | relative_url }}', + '{{ "/sw.js" | relative_url }}', - /* --- HTML --- */ - '{{ "/index.html" | relative_url }}', - '{{ "/404.html" | relative_url }}', + /* --- HTML --- */ + '{{ "/index.html" | relative_url }}', + '{{ "/404.html" | relative_url }}', - {% for tab in site.tabs %} - '{{ tab.url | relative_url }}', - {% endfor %} + {% for tab in site.tabs %} + '{{ tab.url | relative_url }}', + {% endfor %} - /* --- Favicons & compressed JS --- */ - {% assign cache_list = site.static_files | where: 'swcache', true %} - {% for file in cache_list %} - '{{ file.path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} - {% endfor %} + /* --- Favicons & compressed JS --- */ + {% assign cache_list = site.static_files | where: 'swcache', true %} + {% for file in cache_list %} + '{{ file.path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} + {% endfor %} ]; /* The request url with below domain will be cached */ const allowedDomains = [ - {% if site.google_analytics.id != empty and site.google_analytics.id %} - 'www.googletagmanager.com', - 'www.google-analytics.com', - {% endif %} + {% if site.google_analytics.id != empty and site.google_analytics.id %} + 'www.googletagmanager.com', + 'www.google-analytics.com', + {% endif %} - '{{ site.url | split: "//" | last }}', + '{{ site.url | split: "//" | last }}', - {% if site.img_cdn contains '//' and site.img_cdn %} - '{{ site.img_cdn | split: '//' | last | split: '/' | first }}', - {% endif %} + {% if site.img_cdn contains '//' and site.img_cdn %} + '{{ site.img_cdn | split: '//' | last | split: '/' | first }}', + {% endif %} - 'fonts.gstatic.com', - 'fonts.googleapis.com', - 'cdn.jsdelivr.net', - 'polyfill.io' + 'fonts.gstatic.com', + 'fonts.googleapis.com', + 'cdn.jsdelivr.net', + 'polyfill.io' ]; /* Requests that include the following path will be banned */ diff --git a/assets/js/pwa/sw.js b/assets/js/pwa/sw.js index 3213b4f..0e22f90 100644 --- a/assets/js/pwa/sw.js +++ b/assets/js/pwa/sw.js @@ -9,82 +9,81 @@ self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}'); const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M%S" }}'; function verifyDomain(url) { - for (const domain of allowedDomains) { - const regex = RegExp(`^http(s)?:\/\/${domain}\/`); - if (regex.test(url)) { - return true; - } + for (const domain of allowedDomains) { + const regex = RegExp(`^http(s)?:\/\/${domain}\/`); + if (regex.test(url)) { + return true; } + } - return false; + return false; } function isExcluded(url) { - for (const item of denyUrls) { - if (url === item) { - return true; - } + for (const item of denyUrls) { + if (url === item) { + return true; } - return false; + } + return false; } -self.addEventListener('install', event => { - event.waitUntil( - caches.open(cacheName).then(cache => { - return cache.addAll(resource); - }) - ); +self.addEventListener('install', (event) => { + event.waitUntil( + caches.open(cacheName).then((cache) => { + return cache.addAll(resource); + }) + ); }); -self.addEventListener('activate', event => { - event.waitUntil( - caches.keys().then(keyList => { - return Promise.all( - keyList.map(key => { - if (key !== cacheName) { - return caches.delete(key); - } - }) - ); +self.addEventListener('activate', (event) => { + event.waitUntil( + caches.keys().then((keyList) => { + return Promise.all( + keyList.map((key) => { + if (key !== cacheName) { + return caches.delete(key); + } }) - ); + ); + }) + ); }); self.addEventListener('message', (event) => { - if (event.data === 'SKIP_WAITING') { - self.skipWaiting(); - } + if (event.data === 'SKIP_WAITING') { + self.skipWaiting(); + } }); -self.addEventListener('fetch', event => { - event.respondWith( - caches.match(event.request).then(response => { - if (response) { - return response; - } +self.addEventListener('fetch', (event) => { + event.respondWith( + caches.match(event.request).then((response) => { + if (response) { + return response; + } - return fetch(event.request).then(response => { - const url = event.request.url; + return fetch(event.request).then((response) => { + const url = event.request.url; - if (event.request.method !== 'GET' || - !verifyDomain(url) || - isExcluded(url)) { - return response; - } + if ( + event.request.method !== 'GET' || + !verifyDomain(url) || + isExcluded(url) + ) { + return response; + } - /* - see: - */ - let responseToCache = response.clone(); + /* see: */ + let responseToCache = response.clone(); - caches.open(cacheName).then(cache => { - /* console.log('[sw] Caching new resource: ' + event.request.url); */ - cache.put(event.request, responseToCache); - }); + caches.open(cacheName).then((cache) => { + /* console.log('[sw] Caching new resource: ' + event.request.url); */ + cache.put(event.request, responseToCache); + }); - return response; - }); - }) - ); + return response; + }); + }) + ); }); -