diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js | |
init commit
Diffstat (limited to 'shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js')
| -rw-r--r-- | shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js new file mode 100644 index 0000000..0d3c610 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js @@ -0,0 +1,85 @@ +import { bindReporter } from './lib/bindReporter.js'; +import { getActivationStart } from './lib/getActivationStart.js'; +import { getVisibilityWatcher } from './lib/getVisibilityWatcher.js'; +import { initMetric } from './lib/initMetric.js'; +import { observe } from './lib/observe.js'; +import { onHidden } from './lib/onHidden.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const reportedMetricIDs = {}; + +/** + * Calculates the [LCP](https://web.dev/lcp/) value for the current page and + * calls the `callback` function once the value is ready (along with the + * relevant `largest-contentful-paint` performance entry used to determine the + * value). The reported value is a `DOMHighResTimeStamp`. + */ +const onLCP = (onReport) => { + const visibilityWatcher = getVisibilityWatcher(); + const metric = initMetric('LCP'); + let report; + + const handleEntries = (entries) => { + const lastEntry = entries[entries.length - 1] ; + if (lastEntry) { + // The startTime attribute returns the value of the renderTime if it is + // not 0, and the value of the loadTime otherwise. The activationStart + // reference is used because LCP should be relative to page activation + // rather than navigation start if the page was prerendered. + const value = Math.max(lastEntry.startTime - getActivationStart(), 0); + + // Only report if the page wasn't hidden prior to LCP. + if (value < visibilityWatcher.firstHiddenTime) { + metric.value = value; + metric.entries = [lastEntry]; + report(); + } + } + }; + + const po = observe('largest-contentful-paint', handleEntries); + + if (po) { + report = bindReporter(onReport, metric); + + const stopListening = () => { + if (!reportedMetricIDs[metric.id]) { + handleEntries(po.takeRecords() ); + po.disconnect(); + reportedMetricIDs[metric.id] = true; + report(true); + } + }; + + // Stop listening after input. Note: while scrolling is an input that + // stop LCP observation, it's unreliable since it can be programmatically + // generated. See: https://github.com/GoogleChrome/web-vitals/issues/75 + ['keydown', 'click'].forEach(type => { + addEventListener(type, stopListening, { once: true, capture: true }); + }); + + onHidden(stopListening, true); + + return stopListening; + } + + return; +}; + +export { onLCP }; +//# sourceMappingURL=getLCP.js.map |
