Fix PWA error caused by service worker store the POST Request as cache key

fix #297, fix #301
This commit is contained in:
Cotes Chung 2021-03-21 15:55:04 +08:00
parent 88aa81dfe5
commit 18f6c6ac4e
2 changed files with 15 additions and 21 deletions

View file

@ -5,24 +5,27 @@ layout: compress
---
const include = [
/* --- CSS --- */
/*--- CSS ---*/
'{{ "/assets/css/style.css" | relative_url }}',
/*--- Javascripts ---*/
/* --- Javascripts --- */
'{{ "/assets/js/dist/home.min.js" | relative_url }}',
'{{ "/assets/js/dist/page.min.js" | relative_url }}',
'{{ "/assets/js/dist/post.min.js" | relative_url }}',
'{{ "/assets/js/dist/categories.min.js" | relative_url }}',
'{{ "/assets/js/data/search.json" | relative_url }}',
'{{ "/app.js" | relative_url }}',
'{{ "/sw.js" | relative_url }}',
/*--- HTML ---*/
/* --- HTML --- */
/* Tabs */
{% for tab in site.tabs %}
'{{ tab.url }}',
{% endfor %}
'{{ "/404.html" | relative_url }}',
/*--- Icons ---*/
/* --- Icons --- */
{%- capture icon_url -%}
{{ "/assets/img/favicons" | relative_url }}
@ -45,15 +48,7 @@ const include = [
'{{ icon_url }}/favicon-16x16.png',
'{{ icon_url }}/ms-icon-144x144.png',
'{{ icon_url }}/manifest.json',
'{{ icon_url }}/browserconfig.xml',
/*--- Others ---*/
'{{ "/assets/js/data/search.json" | relative_url }}',
'{{ "/404.html" | relative_url }}',
'{{ "/app.js" | relative_url }}',
'{{ "/sw.js" | relative_url }}'
'{{ icon_url }}/browserconfig.xml'
];
const exclude = [

11
sw.js
View file

@ -7,7 +7,6 @@ self.importScripts('{{ "/assets/js/data/cache-list.js" | relative_url }}');
var cacheName = 'chirpy-{{ "now" | date: "%Y%m%d.%H%M" }}';
function isExcluded(url) {
const regex = /(^http(s)?|^\/)/; /* the regex for CORS url or relative url */
for (const rule of exclude) {
@ -19,7 +18,6 @@ function isExcluded(url) {
return false;
}
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil(
@ -29,25 +27,26 @@ self.addEventListener('install', (e) => {
);
});
self.addEventListener('fetch', (e) => {
e.respondWith(
caches.match(e.request).then((r) => {
/* console.log('[Service Worker] Fetching resource: ' + e.request.url); */
/* console.log(`[sw] method: ${e.request.method}, fetching: ${e.request.url}`); */
return r || fetch(e.request).then((response) => {
return caches.open(cacheName).then((cache) => {
if (!isExcluded(e.request.url)) {
/* console.log('[Service Worker] Caching new resource: ' + e.request.url); */
if (e.request.method === "GET") {
/* console.log('[sw] Caching new resource: ' + e.request.url); */
cache.put(e.request, response.clone());
}
}
return response;
});
});
})
);
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keyList) => {