当前位置:网站首页>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的参数是否相同。(这并不是原因)
未实际查明原因。
样本较少,若有错误或补充,敬请指出更正。
边栏推荐
- Kind of weird!Access the destination URL, the host can container but not
- Nodejs installation tutorial
- GCC编译器技术解析
- abaqus如何快速导入其他cae文件的assembly?
- yml字符串读取时转成数字了怎么解决
- 实例030:回文数
- 数据库概论之MySQL表的增删改查1
- Specified URL is not reachable,caused by :‘Read timed out
- 【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
- PHP Warning: putenv() has been disabled for security reasons in phar
猜你喜欢
![[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir](/img/c5/2c42e26e577506573985b30669ca6c.png)
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir

实例032:反向输出II

Nodejs installation and global configuration (super detailed)

8/1 思维+扩展欧几里得+树上dp

Pagoda+FastAdmin 404 Not Found

PMP新考纲考试内容介绍

张驰课堂:六西格玛培训工具——箱线图

张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法

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

APP专项测试:流量测试
随机推荐
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
postgres 多个变量填充字符串,字串格式化
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
堡垒机、堡垒机的原理
.NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
PMP新考纲通关秘籍,告别抓瞎
APP专项测试:流量测试
ASP.NET Core Web API 幂等性
Kind of weird!Access the destination URL, the host can container but not
chrome plugin development guide
[Dataset][VOC] Eyewear dataset 6000 in VOC format
看图就懂|衡量业务增长健康的销售指标如何选择
chrome 插件开发指南
Summer Summary (3)
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
Vscode连接远程服务器出现‘Acquiring lock on/home/~’问题
Node installation and environment variable configuration
解决:- SPY: No data found for this date range, symbol may be delisted报错
FaceBook社媒营销高效转化技巧分享
Expert Insights | 3 ways to seize innovation opportunities in a downturn