Reduce PWA storage space
This commit is contained in:
parent
588fbbbec7
commit
a03149cd40
2 changed files with 71 additions and 45 deletions
|
@ -4,17 +4,18 @@ layout: compress
|
||||||
# The list to be cached by PWA
|
# The list to be cached by PWA
|
||||||
---
|
---
|
||||||
|
|
||||||
const include = [
|
const resource = [
|
||||||
/* --- CSS --- */
|
|
||||||
|
|
||||||
|
/* --- CSS --- */
|
||||||
'{{ "/assets/css/style.css" | relative_url }}',
|
'{{ "/assets/css/style.css" | relative_url }}',
|
||||||
|
|
||||||
/* --- Javascripts --- */
|
/* --- JavaScripts --- */
|
||||||
'{{ "/assets/js/dist/home.min.js" | relative_url }}',
|
{% assign js_path = "/assets/js" | relative_url %}
|
||||||
'{{ "/assets/js/dist/page.min.js" | relative_url }}',
|
'{{ js_path }}/dist/home.min.js',
|
||||||
'{{ "/assets/js/dist/post.min.js" | relative_url }}',
|
'{{ js_path }}/dist/page.min.js',
|
||||||
'{{ "/assets/js/dist/categories.min.js" | relative_url }}',
|
'{{ js_path }}/dist/post.min.js',
|
||||||
'{{ "/assets/js/data/search.json" | relative_url }}',
|
'{{ js_path }}/dist/categories.min.js',
|
||||||
|
'{{ js_path }}/data/search.json',
|
||||||
'{{ "/app.js" | relative_url }}',
|
'{{ "/app.js" | relative_url }}',
|
||||||
'{{ "/sw.js" | relative_url }}',
|
'{{ "/sw.js" | relative_url }}',
|
||||||
|
|
||||||
|
@ -25,12 +26,8 @@ const include = [
|
||||||
'{{ tab.url }}',
|
'{{ tab.url }}',
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
|
||||||
/* --- Icons --- */
|
/* --- Icons --- */
|
||||||
|
{% assign icon_url = "/assets/img/favicons" | relative_url %}
|
||||||
{%- capture icon_url -%}
|
|
||||||
{{ "/assets/img/favicons" | relative_url }}
|
|
||||||
{%- endcapture -%}
|
|
||||||
'{{ icon_url }}/favicon.ico',
|
'{{ icon_url }}/favicon.ico',
|
||||||
'{{ icon_url }}/apple-icon.png',
|
'{{ icon_url }}/apple-icon.png',
|
||||||
'{{ icon_url }}/apple-icon-precomposed.png',
|
'{{ icon_url }}/apple-icon-precomposed.png',
|
||||||
|
@ -52,10 +49,24 @@ const include = [
|
||||||
'{{ icon_url }}/browserconfig.xml'
|
'{{ icon_url }}/browserconfig.xml'
|
||||||
];
|
];
|
||||||
|
|
||||||
const exclude = [
|
/* The request url with below domain will be cached */
|
||||||
{%- if site.google_analytics.pv.proxy_endpoint -%}
|
const allowedDomains = [
|
||||||
'https://{{ site.google_analytics.pv.proxy_endpoint | replace: "https://", "" | split: "/" | first }}',
|
{% if site.google_analytics.id != '' %}
|
||||||
{%- endif -%}
|
'www.googletagmanager.com',
|
||||||
'https://img.shields.io',
|
'www.google-analytics.com',
|
||||||
'/assets/js/data/pageviews.json'
|
{% endif %}
|
||||||
|
|
||||||
|
'{{ site.url | split: "//" | last }}',
|
||||||
|
|
||||||
|
'fonts.gstatic.com',
|
||||||
|
'fonts.googleapis.com',
|
||||||
|
'cdn.jsdelivr.net',
|
||||||
|
'polyfill.io'
|
||||||
|
];
|
||||||
|
|
||||||
|
/* Requests that include the following path will be banned */
|
||||||
|
const denyUrls = [
|
||||||
|
{% if site.google_analytics.pv.cache_path %}
|
||||||
|
'{{ site.google_analytics.pv.cache_path | absolute_url }}'
|
||||||
|
{% endif %}
|
||||||
];
|
];
|
57
sw.js
57
sw.js
|
@ -3,42 +3,55 @@ layout: compress
|
||||||
# PWA service worker
|
# PWA service worker
|
||||||
---
|
---
|
||||||
|
|
||||||
self.importScripts('{{ "/assets/js/data/cache-list.js" | relative_url }}');
|
self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}');
|
||||||
|
|
||||||
var cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
|
const cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
|
||||||
|
|
||||||
|
function verifyDomain(url) {
|
||||||
|
for (const domain of allowedDomains) {
|
||||||
|
const regex = RegExp(`^http(s)?:\/\/${domain}\/`);
|
||||||
|
if (regex.test(url)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function isExcluded(url) {
|
function isExcluded(url) {
|
||||||
const regex = /(^http(s)?|^\/)/; /* the regex for CORS url or relative url */
|
for (const item of denyUrls) {
|
||||||
for (const rule of exclude) {
|
if (url === item) {
|
||||||
if (!regex.test(url) ||
|
|
||||||
url.indexOf(rule) != -1) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addEventListener('install', (e) => {
|
self.addEventListener('install', e => {
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
e.waitUntil(
|
e.waitUntil(
|
||||||
caches.open(cacheName).then((cache) => {
|
caches.open(cacheName).then(cache => {
|
||||||
return cache.addAll(include);
|
return cache.addAll(resource);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', (e) => {
|
self.addEventListener('fetch', e => {
|
||||||
e.respondWith(
|
e.respondWith(
|
||||||
caches.match(e.request).then((r) => {
|
caches.match(e.request).then(r => {
|
||||||
/* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */
|
/* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */
|
||||||
return r || fetch(e.request).then((response) => {
|
return r || fetch(e.request).then(response => {
|
||||||
return caches.open(cacheName).then((cache) => {
|
const url = e.request.url;
|
||||||
if (!isExcluded(e.request.url)) {
|
|
||||||
if (e.request.method === "GET") {
|
if (e.request.method !== 'GET'
|
||||||
|
|| !verifyDomain(url)
|
||||||
|
|| isExcluded(url)) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
return caches.open(cacheName).then(cache => {
|
||||||
/* console.log('[sw] Caching new resource: ' + e.request.url); */
|
/* console.log('[sw] Caching new resource: ' + e.request.url); */
|
||||||
cache.put(e.request, response.clone());
|
cache.put(e.request, response.clone());
|
||||||
}
|
|
||||||
}
|
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -47,14 +60,16 @@ self.addEventListener('fetch', (e) => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', (e) => {
|
self.addEventListener('activate', e => {
|
||||||
e.waitUntil(
|
e.waitUntil(
|
||||||
caches.keys().then((keyList) => {
|
caches.keys().then(keyList => {
|
||||||
return Promise.all(keyList.map((key) => {
|
return Promise.all(
|
||||||
|
keyList.map(key => {
|
||||||
if(key !== cacheName) {
|
if(key !== cacheName) {
|
||||||
return caches.delete(key);
|
return caches.delete(key);
|
||||||
}
|
}
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue