简体   繁体   English

如果响应是404,如何使用Service Worker来缓存跨域资源?

[英]how to use Service Worker to cache cross domain resources if the response is 404?

w3: W3:

6.2 Cross-Origin Resources and CORS¶ 6.2跨源资源和CORS¶
Applications tend to cache items that come from a CDN or other origin. 应用程序倾向于缓存来自CDN或其他来源的项目。 It is possible to request many of them directly using <script>, <img>, <video> and <link> elements. 可以使用<script>, <img>, <video><link>元素直接请求其中许多元素。 It would be hugely limiting if this sort of runtime collaboration broke when offline. 如果这种运行时协作在离线时破坏,那将是极大的限制。 Similarly, it is possible to XHR many sorts of off-origin resources when appropriate CORS headers are set. 类似地,当设置适当的CORS头时,XHR可能有许多种类的非原始资源。

ServiceWorkers enable this by allowing Caches to fetch and cache off-origin items. ServiceWorkers通过允许Caches获取和缓存非原始项来实现此功能。 Some restrictions apply, however. 但是,有一些限制。 First, unlike same-origin resources which are managed in the Cache as Response objects with the type attribute set to "basic", the objects stored are Response objects with the type attribute set to "opaque". 首先,与在Cache中作为Response属性设置为“basic”的Response对象管理的同源资源不同,存储的对象是类型属性设置为“opaque”的Response对象。 Responses typed "opaque" provide a much less expressive API than Responses typed "basic"; 类型为“不透明”的响应提供的API比响应类型为“基本”的表达要少得多; the bodies and headers cannot be read or set, nor many of the other aspects of their content inspected. 无法读取或设置主体和标题,也不检查其内容的许多其他方面。 They can be passed to event.respondWith(r) method in the same manner as the Responses typed "basic", but cannot be meaningfully created programmatically. 它们可以以与响应类型为“基本”相同的方式传递给event.respondWith(r)方法,但不能以编程方式有意义地创建。 These limitations are necessary to preserve the security invariants of the platform. 这些限制对于保留平台的安全不变量是必要的。 Allowing Caches to store them allows applications to avoid re-architecting in most cases. 允许缓存存储它们允许应用程序在大多数情况下避免重新构建。

I have set the CORS header like: 我已将CORS标题设置为:

Access-Control-Allow-Origin:https://xxx.xx.x.com
Access-Control-Allow-Credentials:true

but I still get an "opaque" response and I cannot ensure the code is 200. If I cache these unsuccessful responses, it will cause some problem. 但我仍然得到一个“不透明”的响应,我无法确保代码是200.如果我缓存这些不成功的响应,它将导致一些问题。

For example, a chum of network causes a 404 to the cross domain resources, and I cache it, then I will always use this 404 cache response even thongth when the network problem is corrected. 例如,一个网络密码导致404到跨域资源,并且我将其缓存,然后在网络问题得到纠正时,我将始终使用此404缓存响应。 The same-origin resource do not have this problem. 同源资源没有这个问题。

The mode of a Request (allegedly) defaults to "no-cors" . Requestmode (据称) 默认为"no-cors" (I say "allegedly" because I believe I've seen situations in which an implicitly created Request used in fetch() results in a CORS-enabled Response .) (我说“据称”因为我相信我已经看到在fetch()使用隐式创建的Request导致启用CORS的Response 。)

So you should be explicit about opting in to CORS if you know that your server supports it: 因此,如果您知道服务器支持CORS,那么您应该明确选择加入CORS:

var corsRequest = new Request(url, {mode: 'cors'});
fetch(corsRequest).then(response => ...); // response won't be opaque.

Given a properly configured remote server, a CORS-enabled Request will result in a Response that has a type of "cors" . 给定正确配置的远程服务器,启用CORS的Request将导致Response具有"cors" type Unlike an "opaque" Response , a "cors" Response will expose the underlying status , body , etc. "opaque" Response"cors" Response将暴露基础statusbody等。

Unfortunately, there's no way to detect it. 不幸的是,没有办法检测到它。

For security reasons, it's explicitly not allowed: https://github.com/whatwg/fetch/issues/14 . 出于安全原因,明确不允许: https//github.com/whatwg/fetch/issues/14

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

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