如何预缓存来自 json 文件的 url 列表? [英] How to precaching an url list coming from a json file?

查看:56
本文介绍了如何预缓存来自 json 文件的 url 列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是 serviceworker.js 中硬编码的 url 列表

instead of a hard coded list of urls in serviceworker.js

workbox.precaching.precacheAndRoute([
  { url: '/index.php',revision: 'index2209'},
  { url: '/images/wifino.svg',revision: 'wifino2209'},
  { url: '/offline.php',revision: 'offline2209'}
]);

我想用 json 文件(用 PHP 生成)添加动态 url.json 文件如下所示(但可以更改):

I want to add the urls dynamic with a json file (generated with PHP). The json file looks like this (but can be changed):

{url: 'm.php?tid=1', revision: '11579804421'},
{url: 'm.php?sid=17', revision: '21579804421'},
{url: 'm.php?sid=3', revision: '31579804421'}

如何读取该文件并将其放入 workbox.precaching.precacheAndRoute 列表中?

How can I read this file and put it to the list of workbox.precaching.precacheAndRoute?

非常感谢!

乌利

推荐答案

你真的不能用 workbox-precaching 做到这一点.它依赖于内联存储在顶级 Service Worker 文件中的 revision 信息,因此对 revision 字段的任何更改都会触发 更新过程在服务工作者生命周期中.这是一种优化,可避免在 install 事件之外重新验证预缓存的 URL.

You really can't do that with workbox-precaching. It relies on the revision information being stored inline within the top-level service worker file, so that any changes to a revision field will trigger the update process in the service worker lifecycle. This is an optimization that avoids needing to have to revalidate precached URLs outside of the install event.

解决此问题的一种方法可能是配置您的 Web 服务器以动态生成您的 service-worker.js 内容,并在对 service-worker.js 的调用中插入任何适当的预缓存清单版本控制信息代码>workbox.precaching.precacheAndRoute().自从我使用 PHP 以来已经有一段时间了,所以我无法想出一些示例代码,但是您基本上想使用用于生成 JSON 的任何逻辑而不是编写它到一个单独的文件,将其内容回显到您的 service-worker.js 中.

One way of working around this might be to configure your web server to dynamically generate your service-worker.js contents, and insert whatever the appropriate precache manifest versioning info would be into the call to workbox.precaching.precacheAndRoute(). It's been a while since I've worked with PHP so I can't come up with some sample code off the top of my head, but you'd basically want to use whatever logic you use to generate your JSON and instead of writing it to a separate file, echo its contents into your service-worker.js.

这篇关于如何预缓存来自 json 文件的 url 列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆