[PWA] Cache Third Party Resources from a CDN in a React PWA
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.
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 || [])
相关文章
- ✍️【React巩固计划】写给自己的useEffect
- React v18.x 在 react-router v6 使用 lazy 动态加载组件实现
- 百度前端必会react面试题汇总
- 一小时入门React
- react源码中的生命周期和事件系统
- React性能优化的8种方式
- 阿里前端二面常考react面试题(必备)_2023-02-28
- react的jsx和React.createElement是什么关系?面试常问5
- react源码分析:组件的创建和更新_2023-02-07
- MySQL删除IN操作: 简易指南(mysql删除in)
- 进行字符串比较使用Oracle IN运算符比较字符串(oracle使用in)
- MySQL中实现嵌套查询的IN关键字用法(mysql嵌套查询in)
- 语法应用Oracle中IN语法的多种方式(oracle中in)
- 为什么我们放弃了Vue?Vue和React深度比较
- 深入剖析MySQL中IN和等于操作的差异与应用(mysql中in和等于)
- MySQL表中使用IN命令优化索引,提高查询效率(mysql中in命中索引)
- 以Oracle IN查询精准定位你要的信息(oracle使用in查询)
- Oracle中列转IN更快更智能的架构方案(oracle中列转列in)
- 利用Oracle中的If In语句减少数据处理时间(oracle中if in)
- 条件Oracle查询使用两个IN条件(oracle 两个in)
- 提升Oracle IN操作中的性能改进(oracle in的性能)