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.