Yes of course.
Since I don’t have a gulp, node or webpack setup I used the Workbox CLI tool, which I installed globaly (on a local Windows Linux Subsystem).
I configured a workbox-config.js which looks like this. This is just the base config.
module.exports = {
globDirectory: ‘Web/_Resources/Static/Packages/Package.Site/Frontend/’,
globPatterns: [
“css/photoswipe.css”,
“css/bootstrap.min.css”,
“js/bootstrap.min.js”,
“js/custom.js”,
“js/main.js”,
“js/lightbox.min.js”,
“img/.png",
"img/.ico”,
“img/*.svg”
],
swDest: ‘“Path to Neos main Folder”/Neos/Web/_Resources/Static/Packages/Package.Site/Frontend/sw.js’,
// Define runtime caching rules.
runtimeCaching: [{
// Match any request that ends with .png, .jpg, .jpeg or .svg.
urlPattern: /.(?:png|jpg|jpeg|svg)$/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'images'
},
}],
};
This generates fine with
workbox generateSW workbox-config.js
Adding a few lines of code to the main .js file
if (‘serviceWorker’ in navigator) {
window.addEventListener(‘load’, () => {
navigator.serviceWorker.register(‘/_Resources/Static/Packages/Package.Site/Frontend/sw.js’, {scope: ‘/’})
.then(registration => {
console.log(Service Worker registered! Scope: ${registration.scope}
);
})
.catch(err => {
console.log(Service Worker registration failed: ${err}
);
});
});
}
With the {scope:‘/’} parameter addd I get the following error:
The path of the provided scope (‘/’) is not under the max scope allowed (‘/_Resources/Static/Packages/Package.Site/Frontend/’). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
Without the scope the service worker registers fine, but only works on files that are in the same directory or further down the hierarchy, like pictures, etc.
Normally service workers must be installed in the web root to give them access to the whole website. The goal is to cache all the web resources and let the service worker handle all the request, so the website is available offline.
I hope this is enough information.
Kind Regards,
Ambros Ecke