【问题标题】:PerformanceResourceTiming.responseStart and PerformanceResourceTiming.requestStart both are 0 for a status ok http requestPerformanceResourceTiming.responseStart 和 PerformanceResourceTiming.requestStart 都为 0 表示状态正常的 http 请求
【发布时间】:2021-01-15 04:33:41
【问题描述】:

我想测量 HTTP 请求的 TTFB(到第一个字节的时间)。我在亚马逊 S3 服务器上托管了一个 0KB 的 pdf 文件,我正在使用 fetch api 来获取它。这是我的代码:

let url = "https://s3-us-west-2.amazonaws.com/speedtest.waveform.com/ping.pdf";
await fetch(url).then(res => res.text());
let perf  = performance.getEntriesByName(url).pop();
console.log("ttfb", perf.responseStart - perf.requestStart);

在 chrome 开发者网络选项卡中,我可以看到请求及其所有时间信息:

但我无法访问 PerformanceResourceTiming 文档中的此信息。事实上,包括requestStartresponseStart 在内的许多属性都未设置。将perf 记录打印到控制台显示:

这很奇怪,因为使用这个网址做同样的事情就很好:"https://speed.cloudflare.com/__down?bytes=0"

【问题讨论】:

标签: javascript performance google-chrome fetch-api resource-timing-api


【解决方案1】:

这是因为 s3 服务器未设置 timing-allow-origin 标头。 Cloudflare 确实设置了该选项,这就是我能够读取 ttfb 值的原因。

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 2021-10-20
    • 2017-09-25
    • 1970-01-01
    • 2016-12-24
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多