From 591162464fb1888949222f227af847b32afd35d0 Mon Sep 17 00:00:00 2001 From: Alexander Fuks Date: Tue, 9 Jul 2024 19:46:22 +0000 Subject: [PATCH 1/3] fix: prevent pageview requests caching --- _javascript/pwa/sw.js | 13 ++++++++++++- assets/js/data/swconf.js | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/_javascript/pwa/sw.js b/_javascript/pwa/sw.js index bc67bd8307b..d005c520fbc 100644 --- a/_javascript/pwa/sw.js +++ b/_javascript/pwa/sw.js @@ -5,7 +5,18 @@ importScripts(`${baseurl}/assets/js/data/swconf.js`); const purge = swconf.purge; function verifyUrl(url) { - const requestPath = new URL(url).pathname; + const requestUrl = new URL(url); + const requestPath = requestUrl.pathname; + + if (!requestUrl.protocol.startsWith('http')) { + return false; + } + + for (const denyUrl of swconf.denyUrls) { + if (requestUrl.href.startsWith(denyUrl)) { + return false; + } + } for (const path of swconf.denyPaths) { if (requestPath.startsWith(path)) { diff --git a/assets/js/data/swconf.js b/assets/js/data/swconf.js index 5c1ed29205a..ed7286ac2a5 100644 --- a/assets/js/data/swconf.js +++ b/assets/js/data/swconf.js @@ -22,6 +22,13 @@ const swconf = { {% endfor %} ], + {%- comment -%} The request url starting with below part will not be cached. {%- endcomment -%} + denyUrls: [ + {% if site.analytics.goatcounter.id != nil and site.pageviews.provider == 'goatcounter' %} + 'https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/' + {% endif %} + ], + {%- comment -%} The request url with below path will not be cached. {%- endcomment -%} denyPaths: [ {% for path in site.pwa.cache.deny_paths %} From 65d467f32d2a5420486f65bb393b5fd298653b85 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:21:35 +0800 Subject: [PATCH 2/3] refactor: improve data definitions --- assets/js/data/swconf.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/assets/js/data/swconf.js b/assets/js/data/swconf.js index ed7286ac2a5..798888a99da 100644 --- a/assets/js/data/swconf.js +++ b/assets/js/data/swconf.js @@ -22,21 +22,24 @@ const swconf = { {% endfor %} ], - {%- comment -%} The request url starting with below part will not be cached. {%- endcomment -%} - denyUrls: [ - {% if site.analytics.goatcounter.id != nil and site.pageviews.provider == 'goatcounter' %} - 'https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/' - {% endif %} - ], + interceptor: { + {%- comment -%} URLs containing the following paths will not be cached. {%- endcomment -%} + paths: [ + {% for path in site.pwa.cache.deny_paths %} + {% unless path == empty %} + '{{ path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} + {% endunless %} + {% endfor %} + ], + + {%- comment -%} URLs containing the following prefixes will not be cached. {%- endcomment -%} + urlPrefixes: [ + {% if site.analytics.goatcounter.id != nil and site.pageviews.provider == 'goatcounter' %} + 'https://{{ site.analytics.goatcounter.id }}.goatcounter.com/counter/' + {% endif %} + ] + }, - {%- comment -%} The request url with below path will not be cached. {%- endcomment -%} - denyPaths: [ - {% for path in site.pwa.cache.deny_paths %} - {% unless path == empty %} - '{{ path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} - {% endunless %} - {% endfor %} - ], purge: false {% else %} purge: true From d0dc38d10de7e1e60295b0e0d89ad059962eae75 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:22:42 +0800 Subject: [PATCH 3/3] refactor: update sw.js --- _javascript/pwa/sw.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/_javascript/pwa/sw.js b/_javascript/pwa/sw.js index d005c520fbc..94b64bf7c8a 100644 --- a/_javascript/pwa/sw.js +++ b/_javascript/pwa/sw.js @@ -3,6 +3,7 @@ import { baseurl } from '../../_config.yml'; importScripts(`${baseurl}/assets/js/data/swconf.js`); const purge = swconf.purge; +const interceptor = swconf.interceptor; function verifyUrl(url) { const requestUrl = new URL(url); @@ -12,13 +13,13 @@ function verifyUrl(url) { return false; } - for (const denyUrl of swconf.denyUrls) { - if (requestUrl.href.startsWith(denyUrl)) { + for (const prefix of interceptor.urlPrefixes) { + if (requestUrl.href.startsWith(prefix)) { return false; } } - for (const path of swconf.denyPaths) { + for (const path of interceptor.paths) { if (requestPath.startsWith(path)) { return false; }