[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 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 || [])
复制代码

 

posted @   Zhentiw  阅读(1076)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-12-28 [Python] Histograms for analysis Daily return
2017-12-28 [React] Pass a function to setState in React
2017-12-28 [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
2017-12-28 [Recompose] Make Reusable React Props Streams with Lenses
2016-12-28 [GraphQL] Use GraphQL's List Type for Collections
2016-12-28 [GraphQL] Use GraphQL's Object Type for Basic Types
2016-12-28 [GraphQL] Create a GraphQL Schema
点击右上角即可分享
微信分享提示