From d08e17457fd739700cc2cf0dcaa660c04ec85649 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 21 Sep 2025 19:53:13 +0200 Subject: [PATCH] Add geenrated resources to be pre-cached by service worker --- assets/search.js | 2 +- assets/sw.js | 3 +++ exampleSite/hugo.toml | 2 +- exampleSite/hugo.yaml | 3 ++- layouts/_partials/docs/html-head.html | 17 ++++++++++------- .../_partials/docs/links/resource-precache.html | 2 ++ 6 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 layouts/_partials/docs/links/resource-precache.html diff --git a/assets/search.js b/assets/search.js index 91add6f..a8f7fe4 100644 --- a/assets/search.js +++ b/assets/search.js @@ -5,7 +5,7 @@ {{ $searchConfig := i18n "bookSearchConfig" | default "{}" }} (function () { - const searchDataURL = '{{ $searchData.RelPermalink }}'; + const searchDataURL = '{{ partial "docs/links/resource-precache" $searchData }}'; const indexConfig = Object.assign({{ $searchConfig }}, { includeScore: true, useExtendedSearch: true, diff --git a/assets/sw.js b/assets/sw.js index 2ff11fc..3157db2 100644 --- a/assets/sw.js +++ b/assets/sw.js @@ -4,6 +4,9 @@ const pages = [ {{ range .Site.AllPages -}} "{{ .RelPermalink }}", {{ end -}} + {{ range $permalink, $ok := site.Store.Get "book-sw-precache" -}} + "{{ $permalink }}", + {{ end -}} {{ end }} ]; diff --git a/exampleSite/hugo.toml b/exampleSite/hugo.toml index 66d5700..6b1550a 100644 --- a/exampleSite/hugo.toml +++ b/exampleSite/hugo.toml @@ -139,7 +139,7 @@ enableGitInfo = true # /!\ This is an experimental feature, might be removed or changed at any time # (Optional, experimental, default false) Enables service worker that caches visited pages and resources for offline use. - BookServiceWorker = true + BookServiceWorker = "precache" # /!\ This is an experimental feature, might be removed or changed at any time # (Optional, experimental, default false) Enables a drop-down menu for translations only if a translation is present. diff --git a/exampleSite/hugo.yaml b/exampleSite/hugo.yaml index 4b6401a..535bd92 100644 --- a/exampleSite/hugo.yaml +++ b/exampleSite/hugo.yaml @@ -124,7 +124,8 @@ params: # /!\ This is an experimental feature, might be removed or changed at any time # (Optional, experimental, default false) Enables service worker that caches visited pages and resources for offline use. - BookServiceWorker: true + # can be false, true or 'precache', precache will try to download all content pages on install. + BookServiceWorker: 'precache' # /!\ This is an experimental feature, might be removed or changed at any time # (Optional, experimental, default false) Enables a drop-down menu for translations only if a translation is present. diff --git a/layouts/_partials/docs/html-head.html b/layouts/_partials/docs/html-head.html index 6a6d0ec..58c8774 100644 --- a/layouts/_partials/docs/html-head.html +++ b/layouts/_partials/docs/html-head.html @@ -1,3 +1,8 @@ +{{ "" | safeHTML }} + @@ -24,14 +29,17 @@ {{- $styles := resources.Get "book.scss" | resources.ExecuteAsTemplate "book.scss" . | css.Sass | resources.Minify | resources.Fingerprint }} - + + + {{- if default true .Site.Params.BookSearch -}} {{- $searchJSFile := printf "%s.search.js" .Language.Lang }} {{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify | resources.Fingerprint }} - + {{ end -}} + {{- if .Site.Params.BookServiceWorker -}} {{- $swJS := resources.Get "sw-register.js" | resources.ExecuteAsTemplate "sw.js" . | resources.Minify | resources.Fingerprint }} @@ -44,11 +52,6 @@ {{ printf `` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }} {{ end -}} -{{ "" | safeHTML }} - {{- define "integrity" -}} {{- if (urls.Parse .Permalink).Host -}} integrity="{{ .Data.Integrity }}" crossorigin="anonymous" diff --git a/layouts/_partials/docs/links/resource-precache.html b/layouts/_partials/docs/links/resource-precache.html new file mode 100644 index 0000000..e254a38 --- /dev/null +++ b/layouts/_partials/docs/links/resource-precache.html @@ -0,0 +1,2 @@ +{{- site.Store.SetInMap "book-sw-precache" .RelPermalink true -}} +{{- return .RelPermalink -}} \ No newline at end of file