简体   繁体   English

Service Worker - 打开网站时不要缓存所有文件

[英]Service Worker - dont cache all files when opening a website

My webapp consists of 10 files in total.我的 webapp 总共包含 10 个文件。 When you open it, 5 files are loaded.当您打开它时,会加载 5 个文件。 If you want more features, 5 more files can be loaded (after clicking a button).如果您想要更多功能,可以再加载 5 个文件(单击按钮后)。 50% of users need only 5 files, the rest needs all 10 files. 50% 的用户只需要 5 个文件,其余的用户需要全部 10 个文件。 I want to provide offline capabilities.我想提供离线功能。

In my Service Worker, I listen to "install" event, where I call cache.addAll(... all 10 files ...);在我的 Service Worker 中,我监听了“安装”事件,在那里我调用了 cache.addAll(... all 10 files ...);

Does it mean, that every visitor will load all 10 files, right when they open my webapp?这是否意味着每个访问者在打开我的 web 应用程序时都会加载所有 10 个文件?

Is there a way to add a file into a ServiceWorker cache only if it has been requested by the webapp over the HTTP?只有当 web 应用程序通过 HTTP 请求文件时,才可以将文件添加到 ServiceWorker 缓存中吗?

Sure—I'd recommend taking a look through https://web.dev/offline-cookbook/当然——我建议你浏览一下https://web.dev/offline-cookbook/

The first pattern, that you're already using, is referred to there as " On install—as a dependency ".您已经在使用的第一个模式在那里被称为“安装时——作为依赖项”。 You don't have to cache all 10 of your files via this method—it's fine to cache 5.您不必通过此方法缓存所有 10 个文件 - 缓存 5 个就可以了。

The pattern you're asking about is referred to as " On network response ".您询问的模式称为“网络响应”。

The main thing you'd want to do when combining those two recipes would be to use the same cache name in both, so that regardless of whether the initial response came from the network at runtime or during install time, it ends up being stored in the same cache.结合这两个配方时,您想要做的主要事情是在两者中使用相同的缓存名称,这样无论初始响应是在运行时还是在安装时来自网络,它最终都会存储在相同的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM