获取API - 重定向有哪些用途:手动 [英] Fetch API - What's the use of redirect: manual

查看:179
本文介绍了获取API - 重定向有哪些用途:手动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在玩Javascript Fetch API。据我了解,默认情况下,所有重定向都是透明处理的,最后我得到了重定向链中最后一次调用的响应。

I've recently been playing with the Javascript Fetch API. As far as I understand, by default all redirects are handled transparently and in the end I get a response from the last call in the redirect chain.

但是,我可以调用使用{redirect:'manual'}获取,在这种情况下,它将返回一个没有可用信息的opaqueredirect响应。来自 https://fetch.spec.whatwg.org/#concept- filtered-response-opaque-redirect

However, I could invoke fetch with {redirect: 'manual'}, in which case it would return an opaqueredirect response with no usable information. From https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect


opaque-redirect过滤响应
是一个过滤后的响应,其类型为opaqueredirect,状态为0,状态消息为空字节序列,标题列表为空,正文为空,预告片为空。

An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", status is 0, status message is the empty byte sequence, header list is empty, body is null, and trailer is empty.

https://fetch.spec.whatwg.org/#http-fetch如果重定向设置为'手动',则表示响应变为opaqueredirect:

https://fetch.spec.whatwg.org/#http-fetch says that a response gets to be opaqueredirect if redirect is set to 'manual':


启用请求的重定向模式:

...

- 手动

    设置对内部响应为actualResponse的opaque-redirect过滤响应的响应。

Switch on request’s redirect mode:
...
- manual
    Set response to an opaque-redirect filtered response whose internal response is actualResponse.

规范还说:


换句话说,不透明的过滤响应和不透明重定向过滤的响应几乎与网络错误无法区分。

In other words, an opaque filtered response and an opaque-redirect filtered response are nearly indistinguishable from a network error.

鉴于这一切,为什么在使用Fetch API时会将一组重定向到手动?对我来说似乎没用。是否存在有用的用例?

Given all this, why would one set redirect to manual when using the Fetch API? To me it seems pretty useless. Are there use cases where this would be useful?

推荐答案

我认为简短的回答是:除非您正在做一些服务工作代码类似于 https://github.com/whatwg/fetch/issues/66描述,您不希望重定向:'手动'

I think the short answer is: unless you’re doing something with service-worker code like what https://github.com/whatwg/fetch/issues/66 describes, you don’t ever want redirect: 'manual'.

更长的答案:

HTML规范似乎要求浏览器在浏览器开始导航到资源时初始将重定向模式设置为 manual ,在那之前...(重新)没有设置重定向模式吗? (在这种情况下,它默认返回跟随。)我不明白为什么规范中的算法会这样做,但猜测它必须与处理有关导航失败的情况。无论如何,我相信这是手动重定向模式的唯一用途。

The HTML spec seems to require browsers to initially set the redirect mode to manual when the browser starts navigating to a resource, before then… (re)doing it with redirect mode unset? (In which case it defaults back to follow.) I don’t understand why the algorithm in the spec does it that way, but guess it must have something to do with handling the case where the navigation fails. Regardless, I believe that’s the only use in any spec for the manual redirect mode.

无论如何,Fetch API设计用于公开浏览器在提取中使用的所有相同基元,但这并不意味着Web应用程序代码中的这些基元总是有很好的用途(与浏览器本身对基元的使用形成对比)。

Anyway, the Fetch API is designed to expose all the same primitives that browsers use in fetches, but that doesn’t mean there are always good uses for those primitives in web-app code (in contrast to the use that browsers themselves make of the primitives).

所以我认为Fetch规范曾经要求,即使你 可以 c>重定向调用API:'manual',如果你这样做,浏览器会抛出 - 我猜是因为当时没有人提出任何有效的理由来为浏览器进行导航以外的任何情况设置它。

So I think the Fetch spec used to require that even though you could call the API with redirect: 'manual', browsers would throw if you did—I guess because back then nobody had yet put forward any valid reason for it to be set for any case other than browsers doing navigations.

但由于 https://github.com,这种行为似乎已经改变了/ whatwg / fetch / issues / 66 描述了一个(角落)案例,其中重定向:'manual'在服务工作者代码中是必需的。

But that behavior seems to have been changed due to https://github.com/whatwg/fetch/issues/66 which describes a (corner) case where redirect: 'manual' is needed in service-worker code.

您可以在Fetch API中设置但在网络应用程序代码中实用性很低的类似情况是模式:'no-cors'。最初添加它只是因为浏览器将其用于某些请求,因此Fetch API公开了它。但这是另一个仅限于服务工作者的实用程序的情况 - 缓存响应以便在以后服务,而无需检查响应(模式:'no-cors'阻止网络应用程序代码执行。

A similar case of something you can set in the Fetch API but with very little utility in web-app code is mode: 'no-cors'. That was initially added just because browsers use it for certain requests, so the Fetch API exposes it. But that’s another case that has limited utility just for service workers—for caching responses to serve back as-is later without any need to examine the responses (which mode: 'no-cors' prevents web-app code from doing).

这篇关于获取API - 重定向有哪些用途:手动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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