0
votes

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:

http://deanhume.com/home/blogpost/create-a-really--really-simple-offline-page-using-service-workers/10135

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.

1
Cannot GET /offline.html?cachebust=1485461215845 - does the host you are trying to get this from have an offline.html at all? - Jaromanda X
yes, I can browse to the file with a direct url - user3253156

1 Answers

0
votes

I've discovered that this error occurs when I run on localhost, when I ported the application to a https server (heroku) the offline.html behaves as expected