Fix PWA error caused by service worker store the POST Request as cache key
fix #297, fix #301
This commit is contained in:
parent
88aa81dfe5
commit
18f6c6ac4e
2 changed files with 15 additions and 21 deletions
|
@ -5,8 +5,8 @@ layout: compress
|
|||
---
|
||||
|
||||
const include = [
|
||||
|
||||
/* --- CSS --- */
|
||||
|
||||
'{{ "/assets/css/style.css" | relative_url }}',
|
||||
|
||||
/* --- Javascripts --- */
|
||||
|
@ -14,13 +14,16 @@ const include = [
|
|||
'{{ "/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 --- */
|
||||
|
||||
/* Tabs */
|
||||
{% for tab in site.tabs %}
|
||||
'{{ tab.url }}',
|
||||
{% endfor %}
|
||||
'{{ "/404.html" | relative_url }}',
|
||||
|
||||
/* --- Icons --- */
|
||||
|
||||
|
@ -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
11
sw.js
|
@ -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) => {
|
||||
|
|
Loading…
Reference in a new issue