“状态代码:200 OK(来自 ServiceWorker)"在 Chrome 网络开发者工具中? [英] "Status Code:200 OK (from ServiceWorker)" in Chrome Network DevTools?

查看:73
本文介绍了“状态代码:200 OK(来自 ServiceWorker)"在 Chrome 网络开发者工具中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉 http 状态代码,但最近我在我的 chrome 调试器中看到了一条奇怪的行.而不是普通的 Status Code:200 OK 我看到了以下内容:Status Code:200 OK(来自 ServiceWorker).

我的猜测是,这只是告诉我 ServiceWorker 以某种方式负责访问此文档,但这只是随机猜测.任何人都可以权威地(没有猜测,提供受人尊敬的资源链接)告诉我这是什么意思以及有什么影响?

解决方案

这是一个常见的混淆源,所以我想更详细一些.

我在

我们的 Service Worker 的 fetch 处理程序将执行以下三件事之一:

  • 如果是对 one.js 的请求,它会针对相同的 URL 触发 fetch() 请求,然后调用 event.respondWith() 使用该响应.屏幕截图中的第一个 one.js 条目,即大小"列中带有(来自 ServiceWorker)"的条目,是由于我们调用了 event.respondWith()fetch 处理程序中.屏幕截图中的第二个 one.js 条目,旁边带有小齿轮图标且大小"列中的(来自缓存)"表示fetch() 在响应事件时在 Service Worker 内部发出的请求.由于实际的 one.js 文件在我截取此屏幕截图时已经在 HTTP 缓存中,您会看到(来自缓存)",但是如果 HTTP 缓存还没有该响应,您会看到实际的网络请求以及响应大小.
  • 如果是对 two.js 的请求,它会凭空"构造一个新的 Response 对象来处理请求.(是的,你可以这样做!)在这种情况下,我们正在调用 event.respondWith(),所以 two.js 有一个条目,其中带有 "(from ServiceWorker)在尺寸"列中.但与 one.js 不同的是,我们没有使用 fetch() 来填充响应,因此网络"面板中没有用于 two.js 的额外条目.
  • 最后,如果它是对 three.js 的请求,我们的 Service Worker 的 fetch 事件处理程序实际上不会调用 event.respondWith().从页面的角度,以及从网络面板的角度来看,该请求没有服务工作者参与,这就是为什么在大小"中只有(来自缓存)"而不是(来自 ServiceWorker)"的原因" 列.

I am familiar with http status codes, but recently I saw a strange line in my chrome debugger. Instead of ordinary Status Code:200 OK I saw the following: Status Code:200 OK (from ServiceWorker).

My guess is that this just tells me that ServiceWorker is somehow responsible for accessing this document, but this is just random guess. Can anyone authoritatively (without guesses, with links to respected resources) tell me what does this mean and what are the implications?

解决方案

This is a common source of confusion, so I wanted to go into a bit more detail.

I have a full working demo in this Gist, and you can view a live version of it thanks to RawGit.

Here's the relevant portion of the service worker code inline, for illustrative purposes:

self.addEventListener('fetch', event => {
  if (event.request.url.endsWith('one.js')) {
    // Requests for one.js will result in the SW firing off a fetch() request,
    // which will be reflected in the DevTools Network panel.
    event.respondWith(fetch(event.request));
  } else if (event.request.url.endsWith('two.js')) {
    // Requests for two.js will result in the SW constructing a new Response object,
    // so there won't be an additional network request in the DevTools Network panel.
    event.respondWith(new Response('// no-op'));
  }

  // Requests for anything else won't trigger event.respondWith(), so there won't be
  // any SW interaction reflected in the DevTools Network panel.
});

And here's what a filtered version of the Network panel looks like in Chrome 48 when that service worker is in control of a page, and requests are made for one.js, two.js, and three.js:

Our service worker's fetch handler will do one of three things:

  • If it's a request for one.js, it will fire off a fetch() request for the same URL, and then call event.respondWith() using that response. The first one.js entry in the screenshot, the one with "(from ServiceWorker)" in the "Size" column, is there by virtue of the fact that we called event.respondWith() inside the fetch handler. The second one.js entry in the screenshot, the one with the little gear icon next to it and "(from cache)" in the "Size" column, represents that fetch() request that was made inside the service worker while responding to the event. Since the actual one.js file was already in the HTTP cache at the point I took this screenshot, you see "(from cache)", but if the HTTP cache didn't have that response already, you would see an actual network request along with the response size.
  • If it's a request for two.js, it will handle the request by constructing a new Response object "from thin air". (Yes, you can do that!) In this case, we are calling event.respondWith(), so there's an entry for two.js with "(from ServiceWorker)" in the "Size" column. But unlike with one.js, we're not using fetch() to populate the response, so there's no additional entry in the Network panel for two.js.
  • Finally, if it's a request for three.js, our service worker's fetch event handler won't actually call event.respondWith(). From the perspective of the page, and also from the perspective of the Network panel, there's no service worker involvement with that request, which is why there's just a "(from cache)" rather than "(from ServiceWorker)" in the "Size" column.

这篇关于“状态代码:200 OK(来自 ServiceWorker)"在 Chrome 网络开发者工具中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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