From 0b1f717eb72cea961c9033b53dd875ef5f444746 Mon Sep 17 00:00:00 2001 From: Alexander Fuks Date: Tue, 9 Jul 2024 19:46:22 +0000 Subject: [PATCH] fix: prevent pageview requests caching --- _config.yml | 6 ++++++ _javascript/pwa/sw.js | 13 ++++++++++++- assets/js/data/swconf.js | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index d862178cf1f..b3701d6fb04 100644 --- a/_config.yml +++ b/_config.yml @@ -139,6 +139,12 @@ pwa: enabled: true # The option for PWA feature (installable) cache: enabled: true # The option for PWA offline cache + # URLs defined here will be excluded from the PWA cache. + # Usually it's value is the part of URL of another website. + # If URL includes the value defined, then it will be excluded. + deny_urls: + - "goatcounter.com/counter" + # - "example.com" # URLs match `*example.com*` will not be cached by the PWA # Paths defined here will be excluded from the PWA cache. # Usually its value is the `baseurl` of another website that # shares the same domain name as the current website. diff --git a/_javascript/pwa/sw.js b/_javascript/pwa/sw.js index bc67bd8307b..f5070022a26 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.includes(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..cd40f759652 100644 --- a/assets/js/data/swconf.js +++ b/assets/js/data/swconf.js @@ -22,6 +22,15 @@ const swconf = { {% endfor %} ], + {%- comment -%} The request url with below url part will not be cached. {%- endcomment -%} + denyUrls: [ + {% for url in site.pwa.cache.deny_urls %} + {% unless url == empty %} + '{{ url }}'{%- unless forloop.last -%},{%- endunless -%} + {% endunless %} + {% endfor %} + ], + {%- comment -%} The request url with below path will not be cached. {%- endcomment -%} denyPaths: [ {% for path in site.pwa.cache.deny_paths %}