当前位置:网站首页>PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
2022-08-02 06:20:00 【JA+】
第一次加载Service Worker后,脱机再刷新,无法获取CacheStorage某些缓存的资源
对于很多网上的例子,监听fetch,并使用:caches.match获取缓存数据时
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.request).then(res => {
...在第一次挂载service worker时,我们使用cache.addAll()来将cacheList中指定路径的资源转移到缓存中。
const cacheList = ['./index.html',...]
self.addEventListener('install', e => {
e.waitUntil(
caches.open(cacheName)
.then(cache => {
return cache.addAll(cacheList);
})
.then(() => self.skipWaiting()),
);
});问题与步骤
- 手动清除Service Worker,及caches缓存。

- 第一次加载页面后,马上脱机,然后再刷新。

- caches.match(e.request) ,一些特殊的资源会返回undefined,也就是找不到。

- 控制台查看缓存是有数据的。

- 联网状态下,刷新一次后,再脱机,则缓存资源能正常获取。
这个可以理解,因为这次刷新是在Service Worker生效的条件下。 那么网页中的资源请求都会被Service Worker 的fetch事件拦截,并以e.request 为key添加到cache缓存中。
调查
获取缓存失败的资源的特征:
- html 页面中<script type="module" >的资源
- css 中通过@font-face 引入的字体文件(.ttf) (验证了background:url引入的图片没有问题)
因此初步得出结论:
以上形式的资源,不能通过caches.match(Response) 的形式获取——由cache.addAll(cacheList) 缓存的数据
解决方案
caches.match(e.request.url),传入静态资源的url,可正常获取缓存数据。
猜测原因
可能是与这些资源第一次缓存时,存入的CacheStorage的 key不匹配导致的。
因为第一次进入的缓存是通过 cache.addAll 缓存,其缓存到cache的key为何值不清楚,导致二次脱机刷新时获取不到资源。而实际上已经有缓存。
截至写这篇文章时(2022-07-27)时,MDN(CacheStorage)上依旧为实验性。
根据CacheStorage.match(request, options) MDN 文档,其方法有第二个参数,options,它可以一定程度控制match的匹配规则。
从options.ignoreSearch 看出,匹配规则中会判断同一个url的参数是否相同。(这并不是原因)
未实际查明原因。
样本较少,若有错误或补充,敬请指出更正。
边栏推荐
- MySQL 23 classic interviews hang the interviewer
- HCIP 第四天
- 速看!PMP新考纲、PMBOK第七版解读
- 有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
- Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
- Go inside the basic knowledge
- Kind of weird!Access the destination URL, the host can container but not
- 实验8 VLAN综合实验
- [npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
- Clapper that can interact with the audience in real time
猜你喜欢

数据库概论-MySQL的数据表的基本操作

Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem

聊天机器人如何提升独立站的营销水平?

optional

Redis 常用命令和基本数据结构(数据类型)

Wuhan 2022 organizing of the high-performance computing added new ecological development of high-performance computing

Node installation and environment variable configuration

享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世

typescript ‘props‘ is declared but its value is never read 解决办法

(部分不懂,笔记整理未完成)【图论】差分约束
随机推荐
能与观众实时互动的Claper
MySQL driver jar package download -- nanny tutorial
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
typescript ‘props‘ is declared but its value is never read 解决办法
实例028:递归求等差数列
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
MySQL Advanced SQL Statements (2)
8/1 思维+扩展欧几里得+树上dp
解决Pytorch模型在Gunicorn部署无法运行或者超时问题
两篇不错的php debug教程
Redis 常用命令和基本数据结构(数据类型)
张驰课堂:六西格玛测量系统的误差分析与判定
C# FileInfo类
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
request.getSession(), the story
File upload vulnerability (2)
【暑期每日一题】洛谷 P1192 台阶问题
实验8 VLAN综合实验
Two good php debug tutorials