Now I want to use the install function from serviceWorker (self.addEventListener('install', function(event) {).
Hence I need the complete list of files to install in my serviceWorker.js file, but I failed telling Hugo to range through content files to create a javascript array of those necessary files.
I tried to define a new outputformat for my serviceWorker but failed.
Add this to your serviceWorker layout: layouts/index.js
// Install the app by preloading all recommandations
self.addEventListener('install', (event) => {
const urlsToPrefetch = [
'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2',
'https://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmEU9fBBc4.woff2',
'/sass/style.css',
'https://cdn.jsdelivr.net/npm/jquery@3.6.1/dist/jquery.slim.min.js',
'https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js',
'https://cdn.jsdelivr.net/npm/djibe-material@4.6.2-1.0/js/material.min.js',
'/js/search.min.3575570338356f92346231966c2b380dfc1d6d652428d9778eb15afb44ae5e9dc94a83a0942ada32b8878ff889a9e736.js',
'https://cdn.jsdelivr.net/npm/apexcharts@3.37.0/dist/apexcharts.min.js',
'https://cdn.jsdelivr.net/npm/mermaid@10.1/+esm',
'https://cdn.jsdelivr.net/npm/ion-rangeslider/js/ion.rangeSlider.min.js',
'/recommandations/',
{{- range $index, $value := where site.RegularPages "Section" "recommandations" -}}
{{ if $index }}, {{ end }}
'{{ .RelPermalink }}'
{{- end -}}
];
event.waitUntil(
caches.open(cacheName)
.then((cache) => {
return cache
.addAll(
urlsToPrefetch.map((urlToPrefetch) => {
return new Request(urlToPrefetch, { mode: "no-cors" });
})
)
.then(() => {
console.log("All resources have been fetched and cached.");
});
})
.catch((error) => {
console.error("Pre-fetching failed:", error);
})
);
});
Problem is: it will preload all files for every visitor.
I need to call this serviceWorker install event only when user clicks an “Install app” button.