From 4dfde193e0c7f7d1dbb002a33c92128fa6ad54c1 Mon Sep 17 00:00:00 2001 From: Philip Walton Date: Mon, 3 May 2021 16:44:31 -0700 Subject: [PATCH] Add workaround for Safari FCP bug --- src/getFCP.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/getFCP.ts b/src/getFCP.ts index d848735d..d068eac9 100644 --- a/src/getFCP.ts +++ b/src/getFCP.ts @@ -44,10 +44,18 @@ export const getFCP = (onReport: ReportHandler, reportAllChanges?: boolean) => { } }; - const po = observe('paint', entryHandler); - if (po) { + // TODO(philipwalton): remove the use of `fcpEntry` once this bug is fixed. + // https://bugs.webkit.org/show_bug.cgi?id=225305 + const fcpEntry = performance.getEntriesByName('first-contentful-paint')[0]; + const po = fcpEntry ? null : observe('paint', entryHandler); + + if (fcpEntry || po) { report = bindReporter(onReport, metric, reportAllChanges); + if (fcpEntry) { + entryHandler(fcpEntry); + } + onBFCacheRestore((event) => { metric = initMetric('FCP'); report = bindReporter(onReport, metric, reportAllChanges);