diff --git a/.changeset/date-now-guard.md b/.changeset/date-now-guard.md new file mode 100644 index 0000000000..2e0ac5e711 --- /dev/null +++ b/.changeset/date-now-guard.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +Guard against presence of older 3rd party javascript libraries which redefine Date.now() diff --git a/.changeset/little-radios-thank.md b/.changeset/little-radios-thank.md new file mode 100644 index 0000000000..ea9c3f609d --- /dev/null +++ b/.changeset/little-radios-thank.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +Guard against redefinition of Date.now by third party libraries which are also present on a page alongside rrweb diff --git a/packages/rrweb/src/record/index.ts b/packages/rrweb/src/record/index.ts index c0e69a025f..563942312f 100644 --- a/packages/rrweb/src/record/index.ts +++ b/packages/rrweb/src/record/index.ts @@ -14,6 +14,7 @@ import { hasShadowRoot, isSerializedIframe, isSerializedStylesheet, + nowTimestamp, } from '../utils'; import type { recordOptions } from '../types'; import { @@ -42,7 +43,7 @@ import { function wrapEvent(e: event): eventWithTime { return { ...e, - timestamp: Date.now(), + timestamp: nowTimestamp(), }; } diff --git a/packages/rrweb/src/record/observer.ts b/packages/rrweb/src/record/observer.ts index ff8eac7cc3..ec27a0792d 100644 --- a/packages/rrweb/src/record/observer.ts +++ b/packages/rrweb/src/record/observer.ts @@ -16,6 +16,7 @@ import { legacy_isTouchEvent, patch, StyleSheetMirror, + nowTimestamp, } from '../utils'; import type { observerParam, MutationBufferParam } from '../types'; import { @@ -180,13 +181,13 @@ function initMoveObserver({ ? evt.changedTouches[0] : evt; if (!timeBaseline) { - timeBaseline = Date.now(); + timeBaseline = nowTimestamp(); } positions.push({ x: clientX, y: clientY, id: mirror.getId(target as Node), - timeOffset: Date.now() - timeBaseline, + timeOffset: nowTimestamp() - timeBaseline, }); // it is possible DragEvent is undefined even on devices // that support event 'drag' diff --git a/packages/rrweb/src/utils.ts b/packages/rrweb/src/utils.ts index 2197796258..604c8810e2 100644 --- a/packages/rrweb/src/utils.ts +++ b/packages/rrweb/src/utils.ts @@ -168,6 +168,15 @@ export function patch( } } +// guard against old third party libraries which redefine Date.now +let nowTimestamp = Date.now; + +if (!(/*@__PURE__*/ /[1-9][0-9]{12}/.test(Date.now().toString()))) { + // they have already redefined it! use a fallback + nowTimestamp = () => new Date().getTime(); +} +export { nowTimestamp }; + export function getWindowScroll(win: Window) { const doc = win.document; return {