哪些限制适用于不透明响应?

2023-04-18前端开发问题
9

本文介绍了哪些限制适用于不透明响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

不透明响应被定义为 Fetch API,并表示在 CORS 未启用.

Opaque responses are defined as part of the Fetch API, and represent the result of a request made to a remote origin when CORS is not enabled.

关于如何使用不透明响应(来自 JavaScript 和作为页面上的资源)存在哪些实际限制和陷阱"?

What practical limitations and "gotchas" exist around how opaque responses can be used, both from JavaScript and as resources on a page?

推荐答案

访问不透明响应的标题/正文

不透明响应最直接的限制是您无法从大多数 Response 类的属性,例如 headers,或调用各种方法Body 接口,如 json()text().这符合不透明响应的黑盒特性.

The most straightforward limitation around opaque responses is that you cannot get meaningful information back from most of the properties of the Response class, like headers, or call the various methods that make up the Body interface, like json() or text(). This is in keeping with the black-box nature of an opaque response.

将不透明响应用作页面上的资源

只要浏览器允许使用非 CORS 跨域资源,就可以将不透明响应用作网页上的资源.这是非 CORS 跨域资源以及因此不透明响应有效的元素子集,改编自 Mozilla 开发者网络文档:

Opaque responses can be used as a resource on a web page whenever the browser allows for a non-CORS cross-origin resource to be used. Here's a subset of elements for which non-CORS cross-origin resources, and therefor opaque responses, are valid, adapted from the Mozilla Developer Network documentation:

  • <脚本>
  • <link rel="stylesheet">
  • <img><video><audio>
  • <object><embed>
  • <iframe>
  • <script>
  • <link rel="stylesheet">
  • <img>, <video>, and <audio>
  • <object> and <embed>
  • <iframe>

不透明响应无效有效的一个值得注意的用例是 字体资源.

A notable use case for which opaque responses are not valid is for font resources.

通常,要确定是否可以将不透明响应用作页面上的特定类型资源,请查看相关规范.例如,HTML 规范说明非 CORS<script> 元素可以使用跨域(即不透明)响应,但有一些限制以防止泄漏错误信息.

In general, to determine whether you can use an opaque response as a particular type of resource on a page, check the relevant specification. For example, the HTML specification explains that non-CORS cross-origin (i.e. opaque) responses can be used for <script> elements, though with some limitations to prevent leaking error information.

不透明的响应和缓存存储 API

一个陷阱"开发人员可能遇到的响应不透明,涉及将它们与 缓存存储 API.有两条背景信息是相关的:

One "gotcha" that developer might run into with opaque responses involves using them with the Cache Storage API. Two pieces of background information are relevant:

  • 不透明的 status 属性响应是 总是设置为 0,无论是否原始请求成功或失败.
  • 缓存存储 API 的 add()/addAll()2XX 范围.

从这两点来看,如果作为 add()/addAll() 调用的一部分执行的请求导致不透明的响应,它将失败被添加到缓存中.

From those two points, it follows that if the request performed as part of the add()/addAll() call results in an opaque response, it will fail to be added to the cache.

您可以通过显式执行 fetch() 然后调用 put() 方法,响应不透明.通过这样做,您实际上是在选择承担您正在缓存的响应可能是您的服务器返回的错误的风险.

You can work around this by explicitly performing a fetch() and then calling the put() method with the opaque response. By doing so, you're effectively opting-in to the risk that the response you're caching might have been an error returned by your server.

const request = new Request('https://third-party-no-cors.com/', {
  mode: 'no-cors',
});

// Assume `cache` is an open instance of the Cache class.
fetch(request).then(response => cache.put(request, response));

不透明的响应和navigator.storage API

为了避免跨域信息的泄漏,在用于计算存储配额限制(即是否抛出 QuotaExceeded 异常)的不透明响应的大小中添加了重要的填充,并由navigator.storage API.

In order to avoid leakage of cross-domain information, there's significant padding added to the size of an opaque response used for calculating storage quota limits (i.e. whether a QuotaExceeded exception is thrown) and reported by the navigator.storage API.

此填充的详细信息因浏览器而异,但对于 Google Chrome,这意味着任何单个缓存的不透明响应对整体存储使用量的最小大小是 大约 7 兆字节.在确定要缓存多少不透明响应时,您应该牢记这一点,因为您很容易超出存储配额限制,比您根据不透明资源的实际大小预期的要快得多.

The details of this padding vary from browser to browser, but for Google Chrome, this means that the minimum size that any single cached opaque response contributes to the overall storage usage is approximately 7 megabytes. You should keep this in mind when determining how many opaque responses you want to cache, since you could easily exceeded storage quota limitations much sooner than you'd otherwise expect based on the actual size of the opaque resources.

这篇关于哪些限制适用于不透明响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13