GoogleChrome / workbox

📦 Workbox: JavaScript libraries for Progressive Web Apps
https://developers.google.com/web/tools/workbox/
MIT License
12.27k stars 805 forks source link

Routes for some 3rd-party resources not working #1447

Closed nasearle closed 6 years ago

nasearle commented 6 years ago

I have the following two routes in my service worker for Google fonts and MDL styles:

workbox.routing.registerRoute(
  new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'googleapis',
    plugins: [
      new workbox.expiration.Plugin({
        maxEntries: 30,
      }),
    ],
  }),
);

workbox.routing.registerRoute(
  new RegExp ('https://code.getmdl.io/(.*)'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'furniture-store-mdl',
    plugins: [
      new workbox.expiration.Plugin({
        maxAgeSeconds: 7 * 24 * 60 * 60 // one week
      })
    ]
  })

Note that the first route is copied directly from Common Recipes, but the cacheFirst strategy is replaced with staleWhileRevalidate.

On the second page-load I can see that both routes are caching the responses, as expected:

screen shot 2018-04-25 at 10 26 51 am

screen shot 2018-04-25 at 10 28 04 am

However, on subsequent loads I see that only the fonts are found in the cache and the MDL request goes on to the network:

screen shot 2018-04-25 at 10 30 38 am

screen shot 2018-04-25 at 10 30 51 am

In the Network tab, I can see requests for Google fonts and MDL CSS are coming from the service worker and then again from workbox-core.dev.js:

screen shot 2018-04-25 at 10 36 39 am

When I take the app offline, neither the fonts nor the MDL CSS appear on the page. The log for the fonts is the same as before (response found in the cache), but fonts are not showing up on the page:

screen shot 2018-04-25 at 10 30 38 am

It looks like the MDL requests are not going through the staleWhileRevalidate route, and instead it logs:

screen shot 2018-04-25 at 11 00 14 am

The result is an unstyled page with no fonts while offline. I tried using networkFirst and get the same issue.

To reproduce the issue:

git clone https://github.com/google-developer-training/pwa-ecommerce-demo.git
cd pwa-ecommerce-demo/solution
git checkout v2-ecommerce-app
npm install
npm run serve

Open browser to localhost:8080

nasearle commented 6 years ago

@gauntface @jeffposnick

jeffposnick commented 6 years ago

I believe you're running into the issue resolved in https://github.com/GoogleChrome/workbox/pull/1422, which was included in the Workbox v3.1.0 release.

When I checked out your code locally and ran it with v3.1.0, things worked as expected.

(Your service worker has the v3.0.0 CDN URL hardcoded, so you were still using that, despite having updated the dependency in package.json.)

nasearle commented 6 years ago

Ah ok great! Thanks Jeff