当前位置:网站首页>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 Advanced Statements (1)
- Day 4 of HCIP
- Leetcode周赛304
- 第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】
- Launch Space on-premises deployment (local) Beta!
- 解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
- Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
- 解决Pytorch模型在Gunicorn部署无法运行或者超时问题
- MySQL driver jar package download -- nanny tutorial
猜你喜欢

(部分不懂,笔记整理未完成)【图论】差分约束

MySQL Advanced - MVCC (ultra-detailed finishing)
![(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints](/img/e0/385579fc8657db8b175318bd739908.gif)
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints

HCIP 第四天

The stock price has repeatedly hit new lows, and the real estate SaaS giant is in trouble. How should Mingyuan Cloud transform and save itself?

宝塔+FastAdmin 404 Not Found

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

武汉高性能计算大会2022举办,高性能计算生态发展再添新动力

APP专项测试:流量测试

MySQL 5.7 installation tutorial (full-step, nanny-level tutorial)
随机推荐
实例027:递归输出
MySQL Advanced SQL Statements
实验8 VLAN综合实验
.NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
pointer arithmetic in c language
张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法
振兴农村循环经济 和数链串起农业“生态链”
Redis 常用命令和基本数据结构(数据类型)
每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?
张驰课堂:六西格玛培训工具——箱线图
能与观众实时互动的Claper
typescript ‘props‘ is declared but its value is never read 解决办法
实例032:反向输出II
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
MySQL driver jar package download -- nanny tutorial
PMP新考纲通关秘籍,告别抓瞎
MySQL - Multi-table query and case detailed explanation
Project development specification
武汉高性能计算大会2022举办,高性能计算生态发展再添新动力