Skip to content

Commit

Permalink
feat(pwa): Get version-cache from config
Browse files Browse the repository at this point in the history
Signed-off-by: Khusika Dhamar Gusti <[email protected]>
  • Loading branch information
Khusika Dhamar Gusti committed Jul 11, 2021
1 parent c824f4b commit 98592e1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion assets/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CACHE_VERSION = 1;
const CACHE_VERSION = new URL(location).searchParams.get("version");
const BASE_CACHE_FILES = ["/css/home.min.css", "/css/page.min.css", "/js/themes.min.js", "/site.webmanifest", "/404.html"];
const OFFLINE_CACHE_FILES = ["/css/page.min.css", "/js/themes.min.js", "/offline/index.html"];
const NOT_FOUND_CACHE_FILES = ["/css/page.min.css", "/js/themes.min.js", "/404.html"];
Expand Down
9 changes: 7 additions & 2 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,13 @@ ignoreErrors = ["err-missing-comment-accesstoken", "err-missing-oembed-accesstok
images = ["/logo.png"]
# whether to enable CSS and JS source mapping
SourceMap = false
# whether to enable PWA support
enablePWA = true

# PWA config
[params.pwa]
# whether to enable PWA support
enable = true
# service-worker version
version = "1.0.0"

# Header config
# 页面头部导航栏配置
Expand Down
2 changes: 1 addition & 1 deletion layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
{{- if ne .Site.Params.footer.enable false -}}
{{- partial "footer.html" . -}}
{{- end -}}
{{- if eq .Site.Params.enablePWA true -}}
{{- if eq .Site.Params.pwa.enable true -}}
{{- partial "pwa.html" . -}}
{{- end -}}
</footer>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
{{- end -}}
{{- end -}}

{{- if eq $.Site.Params.enablePWA true -}}
{{- if eq $.Site.Params.pwa.enable true -}}
{{- dict "Source" "sw.js" "Minify" true "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}

Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/pwa.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/sw.min.js', { scope: '/' })
.register('/sw.min.js?version={{.Site.Params.pwa.version}}', { scope: '/' })
.then(() => {
console.info('{{- .Site.Title -}}\u00A0Service Worker Registered');
}, err => console.error('{{- .Site.Title -}}\u00A0Service Worker registration failed: ', err));
Expand Down

0 comments on commit 98592e1

Please sign in to comment.