使用Service Worker时如何处理.htaccess文件中的缓存 [英] How to handle Cache in .htaccess file when using a Service Worker

查看:95
本文介绍了使用Service Worker时如何处理.htaccess文件中的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次为PWA编码,想知道我的 .htaccess 缓存策略是否不会与我的Service Worker状态冲突.这是我的 .htaccess 文件

I'm coding a PWA for the first time and wonder if my .htaccess cache policy isn't about to conflict with my Service Worker Stategies. Here is how it looks in my .htaccess file

<IfModule mod_expires.c>

    ExpiresActive on

    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"

    ExpiresByType text/css "access plus 1 days"
</IfModule>

我应该删除该部分以使我的Service Worker正确处理缓存吗?

Should I remove that part in order to make my Service Worker handle cache properly ?

推荐答案

冲突"的意思在这里不是很清楚.这完全取决于您的意思.拥有99.99%的确定性,没有任何类型的冲突",这些天生就是彼此独立的技术,不会踩到彼此的脚趾.请注意,Service Worker是一个可编写脚本的界面,因此没有单个Service Worker.您的软件可以做的事情与我的完全不同.

Thea meaning of "conflict" is not very clear here. It depends completely on what you mean by that. With 99.99 % certainty there's no "conflict" of any kind, these are inherently separate technologies that don't step on each others' toes. Be aware that Service Worker is a scriptable interface so there's no single Service Worker. Your SW could do something completely different than mine.

请记住,缓存的工作方式如下:

It is good to remember that caches work like this:

没有服务人员:浏览器<->浏览器的HTTP缓存(HTTP标头)<-> HTTP服务器

Without Service Worker: Browser <--> Browser's HTTP cache (HTTP headers) <--> HTTP Server

与服务人员一起:浏览器<->服务人员<->浏览器的HTTP缓存(HTTP标头)<-> HTTP服务器

With Service Worker: Browser <--> Service Worker <--> Browser's HTTP cache (HTTP headers) <--> HTTP Server

现在,在您的情况下,.htaccess是将HTTP缓存标头附加到HTTP服务器提供的响应的东西.因此,当Service Worker与HTTP服务器联系时,这些缓存头将被Service Worker看到.他们不是在冲突".它们不是一种非此即彼"的情况.他们正在做不同的事情.

Now, in your case, the .htaccess is the thing that attaches HTTP caching headers to the responses served by your HTTP server. So those caching headers will be seen by the Service Worker when it contacts the HTTP server. They're not "conflicting". They're not an "either/or" type of a situation. They're doing different things.

示例:

  1. 服务器具有app.css
  2. 浏览器要求输入app.css
  3. 服务人员看到对app.css的请求
  4. 服务人员将请求发送到HTTP服务器
  5. 请求通过浏览器的HTTP接口和缓存
  6. HTTP服务器以app.css响应并说:将其缓存一个月!"
  7. 响应通过浏览器的HTTP界面返回并保存在HTTP缓存中
  8. 服务人员也许会对响应做些
  9. 服务人员将响应转发回浏览器
  10. 浏览器现在具有app.css

第8点:这时,通常 SW将响应缓存在Cache API中(这是单独的,不是HTTP缓存).第3点:此时,通常 SW检查Cache API以查看请求是否已被缓存(如果发生了上述情况).如果Cache API提供了答案,SW将跳过步骤4–8,仅以缓存的版本进行响应.

Point 8: at this point usually the SW caches the response in the Cache API (this is separate, not HTTP cache). Point 3: at this point usually the SW checks the Cache API to see whether the request has already been cached (if above has happened). If Cache API provides the answer, SW skips steps 4–8 and just responds with the cached version.

但是,由于Serwice Worker是可编写脚本的,因此它可能在做其他事情.

But, because the Serwice Worker is scriptable, it might be doing something different.

我真的建议您阅读有关不同HTTP缓存方案以及它们如何与Service Worker一起使用的博客文章:

I REALLY RECOMMEND you read this blog post about different HTTP caching schemes and how they work with Service Worker: https://jakearchibald.com/2016/caching-best-practices/

这篇关于使用Service Worker时如何处理.htaccess文件中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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