From 263dce633e7825af95bfc31c4106358717a7a22a Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 8 Feb 2019 15:22:25 +0330 Subject: [PATCH] chore: improve offline page handling and tests --- packages/workbox/templates/sw.js | 2 +- test/__snapshots__/pwa.test.js.snap | 3 ++- test/fixture/static/offline.html | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/fixture/static/offline.html diff --git a/packages/workbox/templates/sw.js b/packages/workbox/templates/sw.js index 3b89c14a..91f38214 100644 --- a/packages/workbox/templates/sw.js +++ b/packages/workbox/templates/sw.js @@ -53,7 +53,7 @@ workbox.precaching.precacheAndRoute(<%= JSON.stringify(options.preCaching, null, <% if (options.offlinePage) { %> // Register router handler for offlinePage -workbox.routing.registerRoute(new RegExp('/.*'), ({event}) => { +workbox.routing.registerRoute(new RegExp('<%= options.pagesURLPattern %>'), ({event}) => { return workbox.strategies.networkOnly().handle({event}) .catch(() => caches.match('<%= options.offlinePage %>')) })<% } %> diff --git a/test/__snapshots__/pwa.test.js.snap b/test/__snapshots__/pwa.test.js.snap index d53fd4f2..4a2cb333 100644 --- a/test/__snapshots__/pwa.test.js.snap +++ b/test/__snapshots__/pwa.test.js.snap @@ -47,6 +47,7 @@ Array [ "fixture/dist/icons", "fixture/dist/icons/index.html", "fixture/dist/index.html", + "fixture/dist/offline.html", ] `; @@ -103,7 +104,7 @@ workbox.routing.registerRoute(new RegExp('https://google.com/.*'), workbox.strat workbox.routing.registerRoute(new RegExp('/_nuxt/(?!.*(__webpack_hmr|hot-update))'), workbox.strategies.cacheFirst ({}), 'GET') // Register router handler for offlinePage -workbox.routing.registerRoute(new RegExp('/.*'), ({event}) => { +workbox.routing.registerRoute(new RegExp('^/(?!.*(__webpack_hmr|hot-update))'), ({event}) => { return workbox.strategies.networkOnly().handle({event}) .catch(() => caches.match('/offline.html')) }) diff --git a/test/fixture/static/offline.html b/test/fixture/static/offline.html new file mode 100644 index 00000000..368acdfa --- /dev/null +++ b/test/fixture/static/offline.html @@ -0,0 +1,8 @@ + + + Offline + + +

Website is Offline :(

+ +