I am trying to implement an offline fallback functionality for pictures and documents that are not available in the cache. The idea is that when the desired pictures or documents are not available, the service worker would serve a predefined fallback picture or document. I am using the recipe described here but it is not working. To test it using the Chrome dev tools I do the following:
- go to localhost:3003/ (where the app is served)
- let the browser register the service worker
- reload the page
- go to the Application tab and Service Worker section and select the offline checkbox
- reload the page again
- get the following error for pictures and documents that are not saved in the cache:
GET http://localhost:3003/assets/img/Innomaps%20en%20el%20Hult%20Prize%202019.jpg net::ERR_INTERNET_DISCONNECTED
service worker code:
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.1.1/workbox-sw.js');
workbox.core.setCacheNameDetails({
prefix: 'innomaps-pwa',
suffix: 'v1',
precache: 'precache-cache',
runtime: 'runtime-cache'
});
workbox.precaching.precacheAndRoute([
// Precaching the home page
{ url: '/', revision: null },
{ url: 'assets/css/styles.css', revision: null },
{ url: 'assets/js/actions.js', revision: null },
{ url: 'assets/img/InnomapsWhite.png', revision: null },
{ url: 'assets/js/bs-init.js', revision: null },
{ url: 'manifest.json', revision: null },
// Precaching the offline page
{ url: '/offline', revision: null },
]);
workbox.googleAnalytics.initialize();
// This feature is not working!!
workbox.recipes.offlineFallback({
pageFallback: '/offline',
imageFallback: 'assets/img/InnomapsWhite.png'
});
the '/' route is serving a simple HTML file.