From 18f6c6ac4e142aad9a190aa236b7adf7f91adaba Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Sun, 21 Mar 2021 15:55:04 +0800 Subject: [PATCH] Fix PWA error caused by service worker store the POST Request as cache key fix #297, fix #301 --- assets/js/data/cache-list.js | 23 +++++++++-------------- sw.js | 13 ++++++------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/assets/js/data/cache-list.js b/assets/js/data/cache-list.js index a35ec2c..f52654b 100644 --- a/assets/js/data/cache-list.js +++ b/assets/js/data/cache-list.js @@ -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 = [ diff --git a/sw.js b/sw.js index dee9898..1b07241 100644 --- a/sw.js +++ b/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); */ - cache.put(e.request, response.clone()); + 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) => {