[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 || [])
[PWA] Cache Third Party Resources from a CDN in a React PWA的更多相关文章
- [PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline
We can view the PWA offline because we are caching the static and CDN assets for the app - but the l ...
- PWA 技术落地!让你的站点(Web)秒变APP(应用程序)
Web应用方兴未艾,我们已经十分习惯习惯了在电脑上进行以自己的工作,而随着众多功能强大的在线网站,我们的Windows的桌面也不再拥挤着各种快捷方式:不光是PC端,在移动端我们也不再在浩如烟海的应用市 ...
- CDN 内容分发网络技术
1.前言 Internet的高速发展,给人们的工作和生活带来了极大的便利,对Internet的服务品质和访问速度要求越来越高,虽然带宽不断增加,用户数量也在不断增加,受Web服务器的负荷和传输距离等因 ...
- PWA学习心得
PWA学习心得 一.什么是PWA Progressive Web App , (渐进式增强 WEB 应用) 简称 PWA ,是提升WebApp的体验的一种新方法,能给用户原生应用的体验. PWA ...
- 干货 | 10分钟玩转PWA
关于PWA PWA(Progressive Web App), 即渐进式web应用.PWA本质上是web应用,目的是通过多项新技术,在安全.性能.体验等方面给用户原生应用的体验.而且无需像原生应用那样 ...
- CDN的基本原理和基础架构
CDN基本原理 最简单的CDN网络由一个DNS服务器和几台缓存服务器组成: ①当用户点击网站页面上的内容URL,经过本地DNS系统解析,DNS系统会最终将域名的解析权交给CNAME指向的CDN专用DN ...
- PWA(Progressive Web App)入门系列:(一)PWA简单介绍
前言 PWA做为一门Google推出的WEB端的新技术,长处不言而喻.但眼下对于相关方面的知识不是非常丰富.这里我推出一下这方面的新手教程系列.提供PWA方面学习. 什么是PWA PWA全称Progr ...
- CDN技术介绍
CDN技术介绍 一.CDN概述 1.1 CDN定义 CDN即Content Delivery Network (内容分发网络).CDN是建立在现有IP网络基础结构之上的一种增值网络.是在应用层部署的一 ...
- cdn服务器
CDN的基本原理和基础架构 CDN是将源站内容分发至最接近用户的节点,使用户可就近取得所需内容,提高用户访问的响应速度和成功率.解决因分布.带宽.服务器性能带来的访问延迟问题,适用于站点加速.点播.直 ...
随机推荐
- Three Garlands~Educational Codeforces Round 35
C. Three Garlands time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ES6特性以及代码demo
块级作用域let if(true){ let fruit = ‘apple’; } consoloe.log(fruit);//会报错,因为let只在if{ }的作用域有效,也就是块级作用域 恒量co ...
- 校内训练0602 习题exercise
[题目大意] f(i)=((Af(i-1)+B)/(Cf(i-1)+D)) mod P. 给出f(0), A, B, C, D, P, n,求f(n). 多组数据T<=1e4 n<=1e1 ...
- 无线网络发射器选址 (NOIP2014)(真·纯模拟)
原题传送门 好吧,如果说D1T1是纯模拟大水题 D2T1就是纯模拟略水题. 这道题首先我们要看一看数据范围.. 0<=n,m<=128 送分也不带这么送的吧.. 二维前缀和,前缀和,二次循 ...
- VLC-开源播放器编译
http://blog.sina.com.cn/s/blog_7b9d64af0101jpvy.html 需要VLC 在iOS开发中,如果你的程序中播放视频,并且多媒体播放功能是你iOS程序的核心功能 ...
- Jquery Dom节点常用操作
select 标签 form提交的时候 提交select标签选中的value值 1. 添加项 $("#select_id").append("<option val ...
- Kubernetes仓库搭建
#生成证书#/etc/pki/tls/openssl.cnf [ v3_ca ]下添加serviceIPsubjectAltName = 169.169.0.11#创建证书169.169.0.11为s ...
- python的优化机制与垃圾回收与gc模块
python属于动态语言,我们可以随意的创建和销毁变量,如果频繁的创建和销毁则会浪费cpu,那么python内部是如何优化的呢? python和其他很多高级语言一样,都自带垃圾回收机制,不用我们去维护 ...
- JAVA快速功能
1.日期格式化 Date date=new Date(); //转换成时间格式12小时制 SimpleDateFormat df_12=new SimpleDateFormat("yyyy- ...
- svn 基本使用
通过脚本删除物理文件已经不存在的文件 svn st | sed -n '/!/p' | awk '{print $2}' | xargs svn delete 或 svn st | awk '{if ...