渲染引擎如何感知资源类型?是否有解释该过程的文档?

How does the rendering engine perceive the resource type? Is there a documentation that explains the process?

我一直在使用 puppeteer 下载 XHR、CSS、HTML 和 JavaScript 等资源,但想知道它背后的过程是什么。当我检查 puppeteer 文档时,它对 request.resourceType() 说了以下内容:

Contains the request's resource type as it was perceived by the rendering engine.

我想知道渲染引擎是如何分类的。它是基于 content-type 还是 request headers?

之所以提出这个问题是因为当我在 Chrome DevTools 中检查 https://google.com 时,主要 HTML 显示为 document 而其他类型为 text/html 被分类为 other.

但这在 Firefox 中被正确分类为 HTML

您可以在Chromium源码中找到对应的源码here

过程如下:

步骤 1:Chrome 尝试使用 mime 类型进行识别

这将识别以下资源类型 (source code):

  • document 对于 text/html
  • stylesheet 对于 text/css
  • image 对于 image/...
  • script 用于 text/...(不包括文档)或包含 script
  • 的 MIME 类型
  • font 如果 mime 类型包括 font

步骤 2:如果这不起作用,Chrome 会尝试使用 URL/file 扩展

来识别资源类型

例子,会识别什么(source code):

  • script 对于 js
  • stylesheet 对于 css
  • image 对于 jpeggiftiff
  • 完整列表请参阅 this code

第 3 步:如果这也不起作用,other 将是资源类型