From 09406520c4c6fe8915ebaf5caa1606a7c1e68dca Mon Sep 17 00:00:00 2001 From: Philip Walton Date: Wed, 6 Oct 2021 16:25:13 -0700 Subject: [PATCH] Add checks to support Opera mini in presto mode --- rollup.config.js | 2 +- src/getFCP.ts | 6 ++++-- src/getFID.ts | 2 +- src/getLCP.ts | 8 ++++---- src/lib/getVisibilityWatcher.ts | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 67742ce1..ae4aef06 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -33,7 +33,7 @@ const configurePlugins = ({module, polyfill = false}) => { }), replace({ values: { - 'self.__WEB_VITALS_POLYFILL__': polyfill, + 'window.__WEB_VITALS_POLYFILL__': polyfill, }, preventAssignment: true, }) diff --git a/src/getFCP.ts b/src/getFCP.ts index 76e3b1b2..9820898d 100644 --- a/src/getFCP.ts +++ b/src/getFCP.ts @@ -44,9 +44,11 @@ export const getFCP = (onReport: ReportHandler, reportAllChanges?: boolean) => { // TODO(philipwalton): remove the use of `fcpEntry` once this bug is fixed. // https://bugs.webkit.org/show_bug.cgi?id=225305 - // Also, the check for `getEntriesByName` is needed to support Opera: + // The check for `getEntriesByName` is needed to support Opera: // https://github.com/GoogleChrome/web-vitals/issues/159 - const fcpEntry = performance.getEntriesByName && + // The check for `window.performance` is needed to support Opera mini: + // https://github.com/GoogleChrome/web-vitals/issues/185 + const fcpEntry = window.performance && performance.getEntriesByName && performance.getEntriesByName('first-contentful-paint')[0]; const po = fcpEntry ? null : observe('paint', entryHandler); diff --git a/src/getFID.ts b/src/getFID.ts index 28c028b8..82ab3358 100644 --- a/src/getFID.ts +++ b/src/getFID.ts @@ -48,7 +48,7 @@ export const getFID = (onReport: ReportHandler, reportAllChanges?: boolean) => { }, true); } - if (self.__WEB_VITALS_POLYFILL__) { + if (window.__WEB_VITALS_POLYFILL__) { // Prefer the native implementation if available, if (!po) { window.webVitals.firstInputPolyfill(entryHandler as FirstInputPolyfillCallback) diff --git a/src/getLCP.ts b/src/getLCP.ts index f2a9a898..1df2f0f7 100644 --- a/src/getLCP.ts +++ b/src/getLCP.ts @@ -23,7 +23,7 @@ import {onHidden} from './lib/onHidden.js'; import {ReportHandler} from './types.js'; -const reportedMetricIDs:Set = new Set(); +const reportedMetricIDs: Record = {}; export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean) => { const visibilityWatcher = getVisibilityWatcher(); @@ -51,10 +51,10 @@ export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean) => { report = bindReporter(onReport, metric, reportAllChanges); const stopListening = () => { - if (!reportedMetricIDs.has(metric.id)) { + if (!reportedMetricIDs[metric.id]) { po.takeRecords().map(entryHandler as PerformanceEntryHandler); po.disconnect(); - reportedMetricIDs.add(metric.id); + reportedMetricIDs[metric.id] = true; report(true); } } @@ -74,7 +74,7 @@ export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean) => { requestAnimationFrame(() => { requestAnimationFrame(() => { metric.value = performance.now() - event.timeStamp; - reportedMetricIDs.add(metric.id); + reportedMetricIDs[metric.id] = true; report(true); }); }); diff --git a/src/lib/getVisibilityWatcher.ts b/src/lib/getVisibilityWatcher.ts index 9b58f8e6..fc61c858 100644 --- a/src/lib/getVisibilityWatcher.ts +++ b/src/lib/getVisibilityWatcher.ts @@ -36,8 +36,8 @@ export const getVisibilityWatcher = () => { // since navigation start. This isn't a perfect heuristic, but it's the // best we can do until an API is available to support querying past // visibilityState. - if (self.__WEB_VITALS_POLYFILL__) { - firstHiddenTime = self.webVitals.firstHiddenTime; + if (window.__WEB_VITALS_POLYFILL__) { + firstHiddenTime = window.webVitals.firstHiddenTime; if (firstHiddenTime === Infinity) { trackChanges(); }