Skip to content

Commit be2daef

Browse files
authored
Fix absent method now on window.Performance (getsentry#2658)
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
1 parent 026dfe9 commit be2daef

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

packages/utils/src/misc.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,22 +382,26 @@ export const crossPlatformPerformance: CrossPlatformPerformance = (() => {
382382
}
383383
}
384384

385-
if (getGlobalObject<Window>().performance) {
386-
// Polyfill for performance.timeOrigin.
387-
//
388-
// While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin
389-
// is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing.
390-
// tslint:disable-next-line:strict-type-predicates
391-
if (performance.timeOrigin === undefined) {
392-
// As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a
393-
// valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME.
394-
// @ts-ignore
395-
// tslint:disable-next-line:deprecation
396-
performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME;
397-
}
385+
const { performance } = getGlobalObject<Window>();
386+
387+
if (!performance || !performance.now) {
388+
return performanceFallback;
389+
}
390+
391+
// Polyfill for performance.timeOrigin.
392+
//
393+
// While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin
394+
// is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing.
395+
// tslint:disable-next-line:strict-type-predicates
396+
if (performance.timeOrigin === undefined) {
397+
// As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a
398+
// valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME.
399+
// @ts-ignore
400+
// tslint:disable-next-line:deprecation
401+
performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME;
398402
}
399403

400-
return getGlobalObject<Window>().performance || performanceFallback;
404+
return performance;
401405
})();
402406

403407
/**

0 commit comments

Comments
 (0)