I've got a static site with service worker onboard that is hosted at GitLab pages and perfectly works offline on my laptop but occasionally doesn't work offline almost in any mobile browser (works in Opera Mini, but not in mobile Chrome). So I'm trying to figure out what's wrong with it.
My service worker registration script which invokes at index.html in the root of my site:
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('./serviceWorker.js').then(function (registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
My service Worker itself:
let cache_v1 = 'cache_v1';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cache_v1)
.then(cache => cache.addAll(
[
'./',
'./index.html',
'./favicon.png',
'./dist/bundle.js',
'./dist/style.css',
'./src/icons/soyuz_apollo192.png',
'./src/icons/soyuz_apollo_apple_icon.png',
'./src/img/asia.png',
'./src/img/pustota.jpeg',
'./src/img/zima-min.jpg'
]
))
)
})
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(matching => {
return matching || fetch(event.request)
})
)
})
Tell me if there is something else you need to know to help