Skip to content

Commit 11faeb6

Browse files
committed
Reduce PWA storage space
1 parent 3cff51c commit 11faeb6

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed
Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ layout: compress
44
# The list to be cached by PWA
55
---
66

7-
const include = [
8-
/* --- CSS --- */
7+
const resource = [
98

9+
/* --- CSS --- */
1010
'{{ "/assets/css/style.css" | relative_url }}',
1111

12-
/* --- Javascripts --- */
13-
'{{ "/assets/js/dist/home.min.js" | relative_url }}',
14-
'{{ "/assets/js/dist/page.min.js" | relative_url }}',
15-
'{{ "/assets/js/dist/post.min.js" | relative_url }}',
16-
'{{ "/assets/js/dist/categories.min.js" | relative_url }}',
17-
'{{ "/assets/js/data/search.json" | relative_url }}',
12+
/* --- JavaScripts --- */
13+
{% assign js_path = "/assets/js" | relative_url %}
14+
'{{ js_path }}/dist/home.min.js',
15+
'{{ js_path }}/dist/page.min.js',
16+
'{{ js_path }}/dist/post.min.js',
17+
'{{ js_path }}/dist/categories.min.js',
18+
'{{ js_path }}/data/search.json',
1819
'{{ "/app.js" | relative_url }}',
1920
'{{ "/sw.js" | relative_url }}',
2021

@@ -25,12 +26,8 @@ const include = [
2526
'{{ tab.url }}',
2627
{% endfor %}
2728

28-
2929
/* --- Icons --- */
30-
31-
{%- capture icon_url -%}
32-
{{ "/assets/img/favicons" | relative_url }}
33-
{%- endcapture -%}
30+
{% assign icon_url = "/assets/img/favicons" | relative_url %}
3431
'{{ icon_url }}/favicon.ico',
3532
'{{ icon_url }}/apple-icon.png',
3633
'{{ icon_url }}/apple-icon-precomposed.png',
@@ -52,10 +49,24 @@ const include = [
5249
'{{ icon_url }}/browserconfig.xml'
5350
];
5451

55-
const exclude = [
56-
{%- if site.google_analytics.pv.proxy_endpoint -%}
57-
'https://{{ site.google_analytics.pv.proxy_endpoint | replace: "https://", "" | split: "/" | first }}',
58-
{%- endif -%}
59-
'https://img.shields.io',
60-
'/assets/js/data/pageviews.json'
52+
/* The request url with below domain will be cached */
53+
const allowedDomains = [
54+
{% if site.google_analytics.id != '' %}
55+
'www.googletagmanager.com',
56+
'www.google-analytics.com',
57+
{% endif %}
58+
59+
'{{ site.url | split: "//" | last }}',
60+
61+
'fonts.gstatic.com',
62+
'fonts.googleapis.com',
63+
'cdn.jsdelivr.net',
64+
'polyfill.io'
65+
];
66+
67+
/* Requests that include the following path will be banned */
68+
const denyUrls = [
69+
{% if site.google_analytics.pv.cache_path %}
70+
'{{ site.google_analytics.pv.cache_path | absolute_url }}'
71+
{% endif %}
6172
];

sw.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,55 @@ layout: compress
33
# PWA service worker
44
---
55

6-
self.importScripts('{{ "/assets/js/data/cache-list.js" | relative_url }}');
6+
self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}');
77

8-
var cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
8+
const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
9+
10+
function verifyDomain(url) {
11+
for (const domain of allowedDomains) {
12+
const regex = RegExp(`^http(s)?:\/\/${domain}\/`);
13+
if (regex.test(url)) {
14+
return true;
15+
}
16+
}
17+
18+
return false;
19+
}
920

1021
function isExcluded(url) {
11-
const regex = /(^http(s)?|^\/)/; /* the regex for CORS url or relative url */
12-
for (const rule of exclude) {
13-
if (!regex.test(url) ||
14-
url.indexOf(rule) != -1) {
22+
for (const item of denyUrls) {
23+
if (url === item) {
1524
return true;
1625
}
1726
}
1827
return false;
1928
}
2029

21-
self.addEventListener('install', (e) => {
30+
self.addEventListener('install', e => {
2231
self.skipWaiting();
2332
e.waitUntil(
24-
caches.open(cacheName).then((cache) => {
25-
return cache.addAll(include);
33+
caches.open(cacheName).then(cache => {
34+
return cache.addAll(resource);
2635
})
2736
);
2837
});
2938

30-
self.addEventListener('fetch', (e) => {
39+
self.addEventListener('fetch', e => {
3140
e.respondWith(
32-
caches.match(e.request).then((r) => {
41+
caches.match(e.request).then(r => {
3342
/* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */
34-
return r || fetch(e.request).then((response) => {
35-
return caches.open(cacheName).then((cache) => {
36-
if (!isExcluded(e.request.url)) {
37-
if (e.request.method === "GET") {
38-
/* console.log('[sw] Caching new resource: ' + e.request.url); */
39-
cache.put(e.request, response.clone());
40-
}
41-
}
43+
return r || fetch(e.request).then(response => {
44+
const url = e.request.url;
45+
46+
if (e.request.method !== 'GET'
47+
|| !verifyDomain(url)
48+
|| isExcluded(url)) {
49+
return response;
50+
}
51+
52+
return caches.open(cacheName).then(cache => {
53+
/* console.log('[sw] Caching new resource: ' + e.request.url); */
54+
cache.put(e.request, response.clone());
4255
return response;
4356
});
4457

@@ -47,14 +60,16 @@ self.addEventListener('fetch', (e) => {
4760
);
4861
});
4962

50-
self.addEventListener('activate', (e) => {
63+
self.addEventListener('activate', e => {
5164
e.waitUntil(
52-
caches.keys().then((keyList) => {
53-
return Promise.all(keyList.map((key) => {
54-
if(key !== cacheName) {
55-
return caches.delete(key);
56-
}
57-
}));
65+
caches.keys().then(keyList => {
66+
return Promise.all(
67+
keyList.map(key => {
68+
if(key !== cacheName) {
69+
return caches.delete(key);
70+
}
71+
})
72+
);
5873
})
5974
);
6075
});

0 commit comments

Comments
 (0)