使用 workbox-webpack-plugin 时,预缓存资产使用什么工作箱策略? [英] What workbox strategy is used for precached assets when using workbox-webpack-plugin?

查看:54
本文介绍了使用 workbox-webpack-plugin 时,预缓存资产使用什么工作箱策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解工作箱和服务工作者.

I'm trying to understand workbox and service-workers.

使用 workbox-webpack-plugin 和 GenerateSW 模式时,预缓存资产使用的工作箱策略是什么?IE.cacheFirst、staleWhileRevalidate 等.因为我似乎没有为预缓存资产设置路由/策略,以便 service-worker 更新这些资产的缓存.那么这些的默认处理程序是什么?

What is the workbox strategy used for precached assets when using the workbox-webpack-plugin and GenerateSW mode? ie. cacheFirst, staleWhileRevalidate, etc. Because it seems that I do not have set up a route/strategy for precached assets in order for the service-worker to update the caches for these assets. So what is the default handler for these?

其次,来自服务器响应的 Cache-Control 标头如何在刷新资产的工作箱策略中发挥作用?如果有的话.

And secondly, how do the Cache-Control headers from the server response play a role in the workbox strategy to refresh the assets? If at all.

回答关于根/index.html 资产的问题对我最有帮助.

Answering the question wrt the root /index.html asset would be most helpful to me.

谢谢.

推荐答案

workbox-webpack-plugin 生成一个调用 workbox.precaching.precacheAndRoute([...])a>,带有一组 URL(以及每个 URL 的可选修订信息).

workbox-webpack-plugin generates a service worker that makes a call to workbox.precaching.precacheAndRoute([...]), with an array of URLs (and optional revision information for each URL).

precacheAndRoute() 的调用做了两件事:

The call to precacheAndRoute() does two things:

  • 预缓存提供的所有网址.

自动为您创建一个 fetch 处理程序,该处理程序将检查传入请求是否与预缓存 URL 匹配,如果匹配,则使用有效的缓存优先策略进行响应.(这是和路由部分.)在匹配时被调用的代码不是字面上的workbox.strategies.cacheFirst(),但大致等效.

Automatically creates a fetch handler for you that will check incoming requests to see if it matches a precached URL, and if so, respond with what's effectively a cache-first strategy. (This is the and route portion.) The code that gets invoked when there's a match isn't literally workbox.strategies.cacheFirst(), but it's roughly equivalent.

Workbox 将 浏览器的 HTTP 缓存 - 它使用该信息作为 URL 内容可能随时间变化的信号.workbox-precaching文档code> 模块更清楚地说明了正在发生的事情.

Workbox will automatically avoid the browser's HTTP cache if there is out-of-band revision information provided for a given entry in the precache manifest—it uses that as a signal that the contents of the URL might change over time. The docs for the workbox-precaching module shed some more light as to what's going on.

这篇关于使用 workbox-webpack-plugin 时,预缓存资产使用什么工作箱策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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