Admittedly I'm pretty green at this stuff. I've tried to get a service worker to display an offline page and I am getting this error when I take the browser offline:
Cannot GET /offline.html?cachebust=1485461215845
I've based my code on this post:
My Index.html script has this in it:
<script type="text/javascript">
// Register the service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
</script>
And my service worker code (sw.js) has this in it:
'use strict';
var cacheVersion = 1;
var currentCache = {
offline: 'offline-cache' + cacheVersion
};
const offlineUrl = 'offline.html';
this.addEventListener('install', event => {
event.waitUntil(
caches.open(currentCache.offline).then(function(cache) {
return cache.addAll([
offlineUrl
]);
})
);
});
this.addEventListener('fetch', event => {
// request.mode = navigate isn't supported in all browsers
// so include a check for Accept: text/html header.
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request.url).catch(error => {
// Return the offline page
return caches.match(offlineUrl);
})
);
}
else{
// Respond with everything else if we can
event.respondWith(caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
}
});
Debugging the code looks like everything is working except (last line to execute is the return caches.match(offlineurl)) but instead of rendering the offline page I get the cachebuster error. Not much on google on cachebuster. Any insight would be appreciated.
Cannot GET /offline.html?cachebust=1485461215845- does the host you are trying to get this from have anoffline.htmlat all? - Jaromanda X