Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Cache Third Party Resources from a CDN in a React PWA

    Chris AchardChris Achard

    Our service worker caches our static assets - but only those assets that are included in our React App. This means that assets like Bootstrap, which we're including from a third-party CDN, won't be included in the cache, because they're not available at webpack compile time.

    We'll change the service worker to add caching for those assets by using workbox's registerRoute method, and a regular expression to match the entire URL of the asset. We'll pick the staleWhileRevalidate cache strategy, though cacheFirst, or networkFirst would also be valid choices.

    Finally, we'll change the name of the cache they are stored by supplying a cacheName parameter.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: We're getting Bootstrap from a CDN over the network. In offline mode it still looks like it's working, but we're getting it from the browser cache and not from the service worker. If we disable the browser cache and reload. Now we don't have Bootstrap anymore.

    You can see that we've lost all our styling on the application. In the network tab you can actually see the failed CDN requests. In sw.js we can define and register a new route for the service worker. The first argument to register route is either a string that matches the resource that we're trying to cache or a regular expression that matches it.

    Here we'll say that anything that starts with HTTPS and ends with .min.css or .min.js is what we want to cache. It's very important when registering a route that it matches the entire URL of the resource that we're trying to cache, otherwise it won't work. The second argument to register a route tells Workbox what strategy to use when caching that resource.

    Common strategies could be cache first or network first. For Bootstrap we'll use stale-while-revalidate. This will serve Bootstrap as first as possible from the cache first. Then update the cache in the background by making the network call also.

    This strategy is good for things that you want to be available quickly and also where it's not 100 percent critical to have the latest version all the time. Let's build and serve that new service worker. If we reload, we can see Bootstrap came from the Web.

    Then if we switch to offline mode and disable the cache, we can see that the application is still styled because Bootstrap was served from the service worker cache. Also, on the application tab if we check the cache, we can see that it was put into the runtime cache and not the pre-cache because pre-cache is just for assets that were put into the pre-cache manifest.

    We can switch that from the runtime cache by adding a cache name option to the Workbox strategy call. Now if we rebuild and serve that and reload it, we can check the application tab to see the new cache called CDN cache where those assets are stored.