Skip to content

Commit bf8a4b9

Browse files
authored
feat: allow bypassing caches (#55)
1 parent 6919bdf commit bf8a4b9

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

assets/mods/pwa/service-worker/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const fallbacksCache = cachePrefix + 'fallbacks'
1919
// Filter the invalid URLs, such as temporary URLs generated by Hugo PostProgress.
2020
const precaches = params.precaches.filter((url) => url.indexOf('__h_pp_l1') !== 0)
2121
debug('precaches', precaches)
22+
debug('bypass', params.bypass)
2223

2324
// Register page route with NetworkFirst strategy.
2425
// There will be a problem with CacheFirst or StaleWhileRevalidate strategy
@@ -39,6 +40,33 @@ registerRoute(
3940

4041
// Register assets routes.
4142
const assets = ['font', 'image', 'script', 'style']
43+
44+
const isBypass = (url) => {
45+
if (params.bypass && params.bypass.includes(url.href)) {
46+
return true
47+
}
48+
49+
return false
50+
}
51+
52+
if (params.bypass) {
53+
registerRoute(
54+
({ request, sameOrigin, url }) => {
55+
if (!isBypass(url)) {
56+
return false
57+
}
58+
59+
// validate origins
60+
if (sameOrigin) {
61+
return true
62+
}
63+
64+
return false
65+
},
66+
new NetworkOnly(),
67+
);
68+
}
69+
4270
for (let i in assets) {
4371
const kind = assets[i]
4472
const cache = params.caches[kind]

layouts/partials/pwa/assets/sw.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{{- $baseURL := partialCached "pwa/functions/baseurl" . }}
33
{{/* JS compile options. */}}
44
{{- $precaches := slice }}
5+
{{- $bypass := slice }}
56
{{- $langs := slice }}
67
{{- $host := .Site.BaseURL }}
78
{{/* Get precaches pages. */}}
@@ -25,6 +26,9 @@
2526
{{- with .Store.Get "pwa-precache" }}
2627
{{- $precaches = $precaches | append (uniq .) }}
2728
{{- end }}
29+
{{- with .Store.Get "pwa-bypass" }}
30+
{{- $bypass = $bypass | append (uniq .) }}
31+
{{- end }}
2832
{{- $pwaParams := .Site.Params.pwa }}
2933
{{/* Get precaches from configuration. */}}
3034
{{- range $pwaParams.precaches }}
@@ -48,6 +52,7 @@
4852
"baseURL" $baseURL
4953
"langs" $langs
5054
"precaches" $precaches
55+
"bypass" $bypass
5156
"debug" $pwaParams.debug
5257
"caches" $pwaParams.caches
5358
"offline_image" $offlineImg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{- if and .Page .URL }}
2+
{{- .Page.Store.Add "pwa-bypass" (slice .URL) }}
3+
{{- end }}

0 commit comments

Comments
 (0)