diff --git a/assets/mods/pwa/service-worker/index.ts b/assets/mods/pwa/service-worker/index.ts index 475cef6..a27dbd9 100644 --- a/assets/mods/pwa/service-worker/index.ts +++ b/assets/mods/pwa/service-worker/index.ts @@ -19,6 +19,7 @@ const fallbacksCache = cachePrefix + 'fallbacks' // Filter the invalid URLs, such as temporary URLs generated by Hugo PostProgress. const precaches = params.precaches.filter((url) => url.indexOf('__h_pp_l1') !== 0) debug('precaches', precaches) +debug('bypass', params.bypass) // Register page route with NetworkFirst strategy. // There will be a problem with CacheFirst or StaleWhileRevalidate strategy @@ -39,6 +40,33 @@ registerRoute( // Register assets routes. const assets = ['font', 'image', 'script', 'style'] + +const isBypass = (url) => { + if (params.bypass && params.bypass.includes(url.href)) { + return true + } + + return false +} + +if (params.bypass) { + registerRoute( + ({ request, sameOrigin, url }) => { + if (!isBypass(url)) { + return false + } + + // validate origins + if (sameOrigin) { + return true + } + + return false + }, + new NetworkOnly(), + ); +} + for (let i in assets) { const kind = assets[i] const cache = params.caches[kind] diff --git a/layouts/partials/pwa/assets/sw.html b/layouts/partials/pwa/assets/sw.html index 285c9b0..d72cb01 100644 --- a/layouts/partials/pwa/assets/sw.html +++ b/layouts/partials/pwa/assets/sw.html @@ -2,6 +2,7 @@ {{- $baseURL := partialCached "pwa/functions/baseurl" . }} {{/* JS compile options. */}} {{- $precaches := slice }} +{{- $bypass := slice }} {{- $langs := slice }} {{- $host := .Site.BaseURL }} {{/* Get precaches pages. */}} @@ -25,6 +26,9 @@ {{- with .Store.Get "pwa-precache" }} {{- $precaches = $precaches | append (uniq .) }} {{- end }} +{{- with .Store.Get "pwa-bypass" }} + {{- $bypass = $bypass | append (uniq .) }} +{{- end }} {{- $pwaParams := .Site.Params.pwa }} {{/* Get precaches from configuration. */}} {{- range $pwaParams.precaches }} @@ -48,6 +52,7 @@ "baseURL" $baseURL "langs" $langs "precaches" $precaches + "bypass" $bypass "debug" $pwaParams.debug "caches" $pwaParams.caches "offline_image" $offlineImg diff --git a/layouts/partials/pwa/functions/bypass.html b/layouts/partials/pwa/functions/bypass.html new file mode 100644 index 0000000..e9d82e9 --- /dev/null +++ b/layouts/partials/pwa/functions/bypass.html @@ -0,0 +1,3 @@ +{{- if and .Page .URL }} + {{- .Page.Store.Add "pwa-bypass" (slice .URL) }} +{{- end }}