{"version":3,"file":"onTTFB.js","sources":["../../../../src/metrics/web-vitals/onTTFB.ts"],"sourcesContent":["/*\n * Copyright 2020 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { WINDOW } from '../../types';\nimport { bindReporter } from './lib/bindReporter';\nimport { getActivationStart } from './lib/getActivationStart';\nimport { getNavigationEntry } from './lib/getNavigationEntry';\nimport { initMetric } from './lib/initMetric';\nimport { whenActivated } from './lib/whenActivated';\nimport type { MetricRatingThresholds, ReportOpts, TTFBMetric } from './types';\n\n/** Thresholds for TTFB. See https://web.dev/articles/ttfb#what_is_a_good_ttfb_score */\nexport const TTFBThresholds: MetricRatingThresholds = [800, 1800];\n\n/**\n * Runs in the next task after the page is done loading and/or prerendering.\n * @param callback\n */\nconst whenReady = (callback: () => void) => {\n if (WINDOW.document?.prerendering) {\n whenActivated(() => whenReady(callback));\n } else if (WINDOW.document?.readyState !== 'complete') {\n addEventListener('load', () => whenReady(callback), true);\n } else {\n // Queue a task so the callback runs after `loadEventEnd`.\n setTimeout(callback);\n }\n};\n\n/**\n * Calculates the [TTFB](https://web.dev/articles/ttfb) value for the\n * current page and calls the `callback` function once the page has loaded,\n * along with the relevant `navigation` performance entry used to determine the\n * value. The reported value is a `DOMHighResTimeStamp`.\n *\n * Note, this function waits until after the page is loaded to call `callback`\n * in order to ensure all properties of the `navigation` entry are populated.\n * This is useful if you want to report on other metrics exposed by the\n * [Navigation Timing API](https://w3c.github.io/navigation-timing/). For\n * example, the TTFB metric starts from the page's [time\n * origin](https://www.w3.org/TR/hr-time-2/#sec-time-origin), which means it\n * includes time spent on DNS lookup, connection negotiation, network latency,\n * and server processing time.\n */\nexport const onTTFB = (onReport: (metric: TTFBMetric) => void, opts: ReportOpts = {}) => {\n const metric = initMetric('TTFB');\n const report = bindReporter(onReport, metric, TTFBThresholds, opts.reportAllChanges);\n\n whenReady(() => {\n const navigationEntry = getNavigationEntry();\n\n if (navigationEntry) {\n // The activationStart reference is used because TTFB should be\n // relative to page activation rather than navigation start if the\n // page was prerendered. But in cases where `activationStart` occurs\n // after the first byte is received, this time should be clamped at 0.\n metric.value = Math.max(navigationEntry.responseStart - getActivationStart(), 0);\n\n metric.entries = [navigationEntry];\n report(true);\n }\n });\n};\n"],"names":["WINDOW","whenActivated","initMetric","bindReporter","getNavigationEntry","getActivationStart"],"mappings":";;;;;;;;;AAyBO,MAAM,cAAA,GAAyC,CAAC,GAAA,EAAK,IAAI;AAMhE,MAAM,SAAA,GAAY,CAAC,QAAA,KAAyB;AAC1C,EAAA,IAAIA,YAAA,CAAO,UAAU,YAAA,EAAc;AACjC,IAAAC,2BAAA,CAAc,MAAM,SAAA,CAAU,QAAQ,CAAC,CAAA;AAAA,EACzC,CAAA,MAAA,IAAWD,YAAA,CAAO,QAAA,EAAU,UAAA,KAAe,UAAA,EAAY;AACrD,IAAA,gBAAA,CAAiB,MAAA,EAAQ,MAAM,SAAA,CAAU,QAAQ,GAAG,IAAI,CAAA;AAAA,EAC1D,CAAA,MAAO;AAEL,IAAA,UAAA,CAAW,QAAQ,CAAA;AAAA,EACrB;AACF,CAAA;AAiBO,MAAM,MAAA,GAAS,CAAC,QAAA,EAAwC,IAAA,GAAmB,EAAC,KAAM;AACvF,EAAA,MAAM,MAAA,GAASE,sBAAW,MAAM,CAAA;AAChC,EAAA,MAAM,SAASC,yBAAA,CAAa,QAAA,EAAU,MAAA,EAAQ,cAAA,EAAgB,KAAK,gBAAgB,CAAA;AAEnF,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,MAAM,kBAAkBC,qCAAA,EAAmB;AAE3C,IAAA,IAAI,eAAA,EAAiB;AAKnB,MAAA,MAAA,CAAO,QAAQ,IAAA,CAAK,GAAA,CAAI,gBAAgB,aAAA,GAAgBC,qCAAA,IAAsB,CAAC,CAAA;AAE/E,MAAA,MAAA,CAAO,OAAA,GAAU,CAAC,eAAe,CAAA;AACjC,MAAA,MAAA,CAAO,IAAI,CAAA;AAAA,IACb;AAAA,EACF,CAAC,CAAA;AACH;;;;;"}