Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/session-replay-browser/src/session-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ export class SessionReplay implements AmplitudeSessionReplay {
if (!shouldRecord || !sessionId || !config) {
return;
}

// NOTE: If there is already an active instance, do not start a new one
if (this.recordCancelCallback) {
return;
}
Comment on lines +533 to +536
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be curious to see how testing goes, but I'm wondering if this will break some assumptions about this function. Specifically, it's more of a start or restart recording events at the moment.

Copy link
Collaborator Author

@jpollock-ampl jpollock-ampl Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 - I will report back from testing. What are the scenarios in which we would want to restart? Focus changes?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Restart recording on focus to ensure that when user


this.stopRecordingEvents();

const recordFunction = await this.getRecordFunction();
Expand Down
12 changes: 10 additions & 2 deletions packages/session-replay-browser/test/session-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,20 @@ describe('SessionReplay', () => {
expect(currentSequenceEvents).toEqual(undefined);
});

test('should stop recording before starting anew', async () => {
test('should not stop recording if already recording', async () => {
await sessionReplay.init(apiKey, mockOptions).promise;
const stopRecordingMock = jest.fn();
sessionReplay.recordCancelCallback = stopRecordingMock;
await sessionReplay.recordEvents();
expect(stopRecordingMock).toHaveBeenCalled();
expect(stopRecordingMock).not.toHaveBeenCalled();
});

test('should stop recording before starting anew if not already recording', async () => {
await sessionReplay.init(apiKey, mockOptions).promise;
const stopRecordingEventsSpy = jest.spyOn(sessionReplay, 'stopRecordingEvents');
sessionReplay.recordCancelCallback = null; // No active recording
await sessionReplay.recordEvents();
expect(stopRecordingEventsSpy).toHaveBeenCalled();
});

test('should stop recording and send events if user opts out during recording', async () => {
Expand Down
Loading