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 registerRoutemethod, 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.

For some CDN files we also want to cache them:

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

To do that, we can update our sw.js config:

workbox.skipWaiting();
workbox.clientsClaim(); // Cache the files from CDN
workbox.routing.registerRoute(
new RegExp('https:.*min\.(css|js)'),
workbox.strategies.staleWhileRevalidate()
) // Cache all static files
workbox.precaching.precacheAndRoute(self.__precacheManifest || [])

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 staleWhileRevalidate. 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. We can also give a cache name so that we can indentify from the Dev tool.

workbox.skipWaiting();
workbox.clientsClaim(); workbox.routing.registerRoute(
new RegExp('https:.*min\.(css|js)'),
workbox.strategies.staleWhileRevalidate({
cacheName: 'cdn-cache'
})
) workbox.precaching.precacheAndRoute(self.__precacheManifest || [])

最新文章

  1. mininet中iperf sever自动退出
  2. Linux C 开发环境配置
  3. Rockey 4加密狗介绍
  4. Mysql event学习
  5. Java第一天学习笔记整理
  6. PHP 面试题数组篇[ 整理中 ]
  7. mysql DATE_ADD DATE_SUB
  8. python字符decode与encode的问题
  9. POJ3297+map字符串处理
  10. ajax提交表单序列化(serialize())数据
  11. 期望dp-hdu-4336-Card Collector
  12. UIView的alpha、hidden、opaque 深入
  13. 基于C#的数据库文件管理助手
  14. 达尔稳usb转RJ45的接口转换器(usb2.0接口)在ubuntu16.04中驱动(r8152)编译安装与使用
  15. UOJ#370. 【UR #17】滑稽树上滑稽果 动态规划
  16. Android预置Apk方法
  17. 导致spring事务配置不起作用的一种原因
  18. vue实战记录(四)- vue实现购物车功能之过滤器的使用
  19. freemarker demo
  20. 一张图说明TCP和UCP协议

热门文章

  1. 基于RRT的机器人自主探索建图
  2. Codeforces Round #325 (Div. 2) A
  3. 7月11日day3总结
  4. 转:android service总结2
  5. HDU5307 He is Flying
  6. [ CodeVS冲杯之路 ] P1501
  7. 废弃sqlite代码,备查
  8. Android控件点击事件
  9. 修改 Lua支持中文变量名
  10. C#中使用aria2c进行下载并显示进度条