Skip to content

Commit

Permalink
fix: prevent pageview requests caching
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfux committed Jul 11, 2024
1 parent 8c30f0a commit 0b1f717
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion _javascript/pwa/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
9 changes: 9 additions & 0 deletions assets/js/data/swconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down

0 comments on commit 0b1f717

Please sign in to comment.