From 37329bc0f8d94e2117b23155fbc018e8cfe8f7a2 Mon Sep 17 00:00:00 2001 From: Alexander Savin Date: Mon, 8 Jun 2020 19:23:05 +0300 Subject: [PATCH] Fix absent method `now` on window.Performance We have an issue in our self-hosted sentry `Object [object Performance] has no method 'now'` It shows `Sentry.addBreadcrumb` as the source of the error. It seems that some browsers can have performance without `now` method. 50% of all events from Chrome 22.0.1229 - I know that it's pretty old - but the fix seems trivial --- packages/utils/src/misc.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index cc63caa8f165..76720fe7575a 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -382,22 +382,26 @@ export const crossPlatformPerformance: CrossPlatformPerformance = (() => { } } - if (getGlobalObject().performance) { - // Polyfill for performance.timeOrigin. - // - // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin - // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. - // tslint:disable-next-line:strict-type-predicates - if (performance.timeOrigin === undefined) { - // As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a - // valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME. - // @ts-ignore - // tslint:disable-next-line:deprecation - performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME; - } + const { performance } = getGlobalObject(); + + if (!performance || !performance.now) { + return performanceFallback; + } + + // Polyfill for performance.timeOrigin. + // + // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin + // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. + // tslint:disable-next-line:strict-type-predicates + if (performance.timeOrigin === undefined) { + // As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a + // valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME. + // @ts-ignore + // tslint:disable-next-line:deprecation + performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME; } - return getGlobalObject().performance || performanceFallback; + return performance; })(); /**