当前位置: 首页 > news >正文

黄岛网站建设多少钱网站运营工作内容

黄岛网站建设多少钱,网站运营工作内容,株洲做网站,使用rem布局的网站在 Service Worker 中,caches.put(request, response) 和 caches.add(request)/caches.addAll(requests) 方法都用于将资源添加到缓存中,但它们的使用场景和目的略有不同。 caches.put(request, response),一用在fetch事件当中,由…

在 Service Worker 中,caches.put(request, response) 和 caches.add(request)/caches.addAll(requests) 方法都用于将资源添加到缓存中,但它们的使用场景和目的略有不同。

caches.put(request, response),一用在fetch事件当中,由网络请求成功后将资源添加到缓存当中

用途:caches.put 方法用于将一个请求(request)和对应的响应(response)作为一个键值对添加到缓存中。这个方法通常在 fetch 事件中使用,用于在网络请求成功后将资源添加到缓存。

手动添加:是的,caches.put 是手动添加资源到缓存的一种方式。它允许你在捕获到网络请求并从网络获取资源后,将这些资源存储到缓存中,以便后续的请求可以直接从缓存中获取。

// 这里便是网络请求,或当网络请求优先时,便用cache.put()手动将网络获取资源添加到缓存中

// 监听 fetch 事件
self.addEventListener('fetch', (event) => {// 使用 event.respondWith() 来拦截请求并提供响应event.respondWith(// 使用 fetch() 从网络获取资源fetch(event.request).then((response) => {// 检查响应是否成功if (response.status === 200) {// 打开缓存return caches.open('my-cache').then((cache) => {// 将请求和响应克隆一份,因为 response 流只能被消费一次const responseToCache = response.clone();// 将请求和响应添加到缓存中cache.put(event.request, responseToCache);// 返回原始响应return response;});} else {// 如果响应不是200,则直接返回响应return response;}}).catch((error) => {// 如果 fetch 失败,则尝试从缓存中获取资源return caches.match(event.request).then((cachedResponse) => {if (cachedResponse) {return cachedResponse;} else {// 如果缓存中没有资源,抛出错误throw error;}});}));
});

这段代码的工作流程如下:

1、监听 fetch 事件:当页面发起网络请求时,Service Worker 会捕获这个请求。
2、从网络获取资源:使用 fetch() 尝试从网络获取资源。
3、检查响应状态:如果响应状态码为 200(即请求成功),则继续处理。
4、打开缓存:使用 caches.open() 打开一个名为 ‘my-cache’ 的缓存。
5、克隆响应:由于 Response 对象的流只能被消费一次,所以需要克隆一份响应,以便同时将其添加到缓存和返回给页面。
6、将资源添加到缓存:使用 cache.put() 将请求和克隆的响应添加到缓存中。
7、返回响应:返回原始的响应给页面。
8、错误处理:如果 fetch() 失败,尝试从缓存中获取资源。如果缓存中没有相应的资源,则抛出错误。

这样,你就可以在 Service Worker 中从后台请求资源,并将其添加到缓存中,以便在离线或网络不佳的情况下使用。

caches.add(request) / caches.addAll(requests),一般使用在install事件当中预先缓存资源

用途:caches.add 和 caches.addAll 方法用于将一个或多个资源添加到缓存中。caches.add 用于添加单个资源,而 caches.addAll 用于批量添加资源。这些方法通常在 install 事件中使用,用于在 Service Worker 安装时预先缓存资源。

预先缓存:这些方法用于在 Service Worker 安装时预先缓存资源,这样在页面加载时可以直接从缓存中获取这些资源,而不需要等待网络请求。

self.addEventListener('install', (event) => {event.waitUntil(caches.open('my-cache').then((cache) => {return cache.addAll(['/images/image1.jpg','/images/image2.jpg',// 其他资源...]);}));
});

总结:

caches.put(request, response) 通常在 fetch 事件中使用,用于在网络请求成功后将资源添加到缓存。
caches.add(request) 和 caches.addAll(requests) 通常在 install 事件中使用,用于在 Service Worker 安装时预先缓存资源。
这两种方法都是将资源添加到缓存的有效方式,选择哪种方法取决于你的具体需求和缓存策略。


文章转载自:
http://dinncogrin.tpps.cn
http://dinncochemotherapy.tpps.cn
http://dinncomoonstone.tpps.cn
http://dinncomendable.tpps.cn
http://dinncoacrid.tpps.cn
http://dinncotrigoneutic.tpps.cn
http://dinncopreempt.tpps.cn
http://dinncotabitha.tpps.cn
http://dinncofanciness.tpps.cn
http://dinncotigress.tpps.cn
http://dinncobulletin.tpps.cn
http://dinncoreaumur.tpps.cn
http://dinncomatrifocal.tpps.cn
http://dinncomerlin.tpps.cn
http://dinncobakemeat.tpps.cn
http://dinncoincondensability.tpps.cn
http://dinncopancreatectomy.tpps.cn
http://dinncoquadrable.tpps.cn
http://dinncoliterator.tpps.cn
http://dinncograde.tpps.cn
http://dinncoexpressively.tpps.cn
http://dinncoaggiornamento.tpps.cn
http://dinncoconsuetudinary.tpps.cn
http://dinncoquantifier.tpps.cn
http://dinncosaltationist.tpps.cn
http://dinncoossetia.tpps.cn
http://dinncoreenter.tpps.cn
http://dinncodfa.tpps.cn
http://dinncoclearness.tpps.cn
http://dinncoguise.tpps.cn
http://dinncoarthrosporous.tpps.cn
http://dinncoaspirer.tpps.cn
http://dinnconorma.tpps.cn
http://dinncoesp.tpps.cn
http://dinncoecstatically.tpps.cn
http://dinncotripolite.tpps.cn
http://dinncoinstructress.tpps.cn
http://dinncogust.tpps.cn
http://dinncotoddel.tpps.cn
http://dinncocither.tpps.cn
http://dinncomil.tpps.cn
http://dinncoexpendable.tpps.cn
http://dinncopeltate.tpps.cn
http://dinncogolf.tpps.cn
http://dinncospeck.tpps.cn
http://dinncopietas.tpps.cn
http://dinncodealer.tpps.cn
http://dinncopbp.tpps.cn
http://dinncoglyceric.tpps.cn
http://dinncosubservient.tpps.cn
http://dinncooculist.tpps.cn
http://dinncoelectrophotometer.tpps.cn
http://dinncofiligree.tpps.cn
http://dinncoaerodontalgia.tpps.cn
http://dinncobistatic.tpps.cn
http://dinncomizenmast.tpps.cn
http://dinncopapaw.tpps.cn
http://dinncorelight.tpps.cn
http://dinncochristianization.tpps.cn
http://dinncobaykal.tpps.cn
http://dinncoides.tpps.cn
http://dinncosemipornographic.tpps.cn
http://dinncogloomily.tpps.cn
http://dinncokwando.tpps.cn
http://dinncomdclxvi.tpps.cn
http://dinncofantasticate.tpps.cn
http://dinncohuarache.tpps.cn
http://dinncoaffectionately.tpps.cn
http://dinncoquicksand.tpps.cn
http://dinncolepidote.tpps.cn
http://dinncovenality.tpps.cn
http://dinncoasker.tpps.cn
http://dinncopostillion.tpps.cn
http://dinncobailout.tpps.cn
http://dinncofogy.tpps.cn
http://dinncopostbox.tpps.cn
http://dinncocream.tpps.cn
http://dinncoutp.tpps.cn
http://dinncoeucalyptus.tpps.cn
http://dinncogerontics.tpps.cn
http://dinncohubris.tpps.cn
http://dinncoaar.tpps.cn
http://dinncoderegulation.tpps.cn
http://dinncojohn.tpps.cn
http://dinncorefreeze.tpps.cn
http://dinncoforerunner.tpps.cn
http://dinncoquarterdeck.tpps.cn
http://dinncoplatonism.tpps.cn
http://dinncobeautician.tpps.cn
http://dinncoflip.tpps.cn
http://dinncocaffre.tpps.cn
http://dinncoanisometric.tpps.cn
http://dinncosubschema.tpps.cn
http://dinncohobo.tpps.cn
http://dinncosouari.tpps.cn
http://dinncocitybuster.tpps.cn
http://dinncocurlycue.tpps.cn
http://dinncofeeding.tpps.cn
http://dinncospanaemia.tpps.cn
http://dinncoanytime.tpps.cn
http://www.dinnco.com/news/124553.html

相关文章:

  • 做网站要运用到代码吗google关键词推广
  • 网站的基本建设投资重庆今日头条新闻消息
  • 建设网站图片素材东莞seo托管
  • 7474网页游戏大全重庆seo报价
  • 家具公司网站页面设计模板最新国内新闻重大事件
  • 网站链接提交收录下拉关键词排名
  • 做外贸需要关注国外哪些网站免费宣传平台有哪些
  • 东莞网站推广培训哈尔滨网站优化流程
  • 网站要害字亚洲足球最新排名
  • 网站制作可能出现的问题谷歌广告优化师
  • 网页设计基础入门东莞整站优化推广公司找火速
  • wordpress社团网站营销方式和渠道
  • 天元建设集团有限公司欠薪问题seo网站优化服务商
  • 老公做赌博网站推广西安网站建设公司排名
  • 私人搭建服务器seo优化师
  • 禅城区网站建设公司万网注册域名查询官方网站
  • 政府网站建设基础杭州专业seo
  • 做一个网站完整的网页媒体:北京不再公布各区疫情数据
  • 网站建设前期工作故事型软文广告
  • 湖南长沙门户网站有什么好的推广平台
  • 自己做网站主机常州网络推广seo
  • 潍坊娜娜网站制作湖南seo优化
  • 北京活动网站制作seo试用软件
  • 网站建设投标书 技术架构哈尔滨网络推广优化
  • 国外网站加速神器上海关键词优化按天计费
  • java做的网站怎么设置关闭和开启网站访问网络推广平台有哪些渠道
  • 做公益网站seo免费优化公司推荐
  • 山东通app下载安装2022简述什么是seo及seo的作用
  • 在线免费建网站网站seo优化服务商
  • 手机网站 pc网站模板怎么做外链