From 979f86cf64e1fcace4231fb070c7e6398fd4e5ec Mon Sep 17 00:00:00 2001 From: Alexander Fuks Date: Wed, 17 Jul 2024 19:32:48 +0400 Subject: [PATCH] fix: avoid caching pageviews data (#1849) --- _javascript/pwa/sw.js | 16 ++++++++++++++-- assets/js/data/swconf.js | 26 ++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/_javascript/pwa/sw.js b/_javascript/pwa/sw.js index bc67bd8..94b64bf 100644 --- a/_javascript/pwa/sw.js +++ b/_javascript/pwa/sw.js @@ -3,11 +3,23 @@ import { baseurl } from '../../_config.yml'; importScripts(`${baseurl}/assets/js/data/swconf.js`); const purge = swconf.purge; +const interceptor = swconf.interceptor; function verifyUrl(url) { - const requestPath = new URL(url).pathname; + const requestUrl = new URL(url); + const requestPath = requestUrl.pathname; - for (const path of swconf.denyPaths) { + if (!requestUrl.protocol.startsWith('http')) { + return false; + } + + for (const prefix of interceptor.urlPrefixes) { + if (requestUrl.href.startsWith(prefix)) { + return false; + } + } + + for (const path of interceptor.paths) { if (requestPath.startsWith(path)) { return false; } diff --git a/assets/js/data/swconf.js b/assets/js/data/swconf.js index 5c1ed29..798888a 100644 --- a/assets/js/data/swconf.js +++ b/assets/js/data/swconf.js @@ -22,14 +22,24 @@ const swconf = { {% endfor %} ], - {%- 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 %} - ], + 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 %} + ] + }, + purge: false {% else %} purge: true