Skip to content

Commit

Permalink
fix(replayer): Fix null objects on playback (#38)
Browse files Browse the repository at this point in the history
These objects can all be null (e.g. if player is playing and we navigate
to a page without player, due to async, the player can be destroyed by
the time we try to access these objects). Remove the non-null assertion
operator as typescript is right that they can be null.

Fixes https://sentry.sentry.io/issues/2628382889/,
https://sentry.sentry.io/issues/3616367681/,
https://sentry.sentry.io/issues/2669497075/,
https://sentry.sentry.io/issues/3549471392/
  • Loading branch information
billyvg authored Feb 8, 2023
1 parent 69f74e7 commit 85f220f
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class Replayer {
this.rebuildFullSnapshot(
firstFullsnapshot as fullSnapshotEvent & { timestamp: number },
);
this.iframe.contentWindow!.scrollTo(
this.iframe.contentWindow && this.iframe.contentWindow.scrollTo(
(firstFullsnapshot as fullSnapshotEvent).data.initialOffset,
);
}, 1);
Expand Down Expand Up @@ -367,7 +367,7 @@ export class Replayer {
}
this.iframe.contentDocument
?.getElementsByTagName('html')[0]
.classList.remove('rrweb-paused');
?.classList.remove('rrweb-paused');
this.emitter.emit(ReplayerEvents.Start);
}

Expand All @@ -381,7 +381,7 @@ export class Replayer {
}
this.iframe.contentDocument
?.getElementsByTagName('html')[0]
.classList.add('rrweb-paused');
?.classList.add('rrweb-paused');
this.emitter.emit(ReplayerEvents.Pause);
}

Expand Down Expand Up @@ -553,7 +553,7 @@ export class Replayer {
this.firstFullSnapshot = true;
}
this.rebuildFullSnapshot(event, isSync);
this.iframe.contentWindow!.scrollTo(event.data.initialOffset);
this.iframe.contentWindow && this.iframe.contentWindow.scrollTo(event.data.initialOffset);
};
break;
case EventType.IncrementalSnapshot:
Expand Down Expand Up @@ -690,8 +690,12 @@ export class Replayer {
documentElement: HTMLElement,
head: HTMLHeadElement,
) {
if (!documentElement) {
return;
}

const styleEl = document.createElement('style');
documentElement!.insertBefore(styleEl, head);
documentElement.insertBefore(styleEl, head);
const injectStylesRules = getInjectStyleRules(
this.config.blockClass,
).concat(this.config.insertStyleRules);
Expand Down Expand Up @@ -1655,14 +1659,16 @@ export class Replayer {
return this.debugNodeNotFound(d, d.id);
}
if ((target as Node) === this.iframe.contentDocument) {
this.iframe.contentWindow!.scrollTo({
this.iframe.contentWindow && this.iframe.contentWindow.scrollTo({
top: d.y,
left: d.x,
behavior: isSync ? 'auto' : 'smooth',
});
} else if (target.__sn.type === NodeType.Document) {
const defaultView = ((target as unknown) as Document).defaultView;

// nest iframe content document
((target as unknown) as Document).defaultView!.scrollTo({
defaultView && defaultView.scrollTo({
top: d.y,
left: d.x,
behavior: isSync ? 'auto' : 'smooth',
Expand Down

0 comments on commit 85f220f

Please sign in to comment.