@@ -524,6 +524,7 @@ export class ReplayContainer implements ReplayContainerInterface {
524524 }
525525
526526 const activityTime = Date . now ( ) ;
527+ const earliestEvent = this . eventBuffer && this . eventBuffer . getEarliestTimestamp ( ) ;
527528
528529 DEBUG_BUILD && logger . info ( 'Converting buffer to session' ) ;
529530
@@ -549,6 +550,9 @@ export class ReplayContainer implements ReplayContainerInterface {
549550
550551 // Once this session ends, we do not want to refresh it
551552 if ( this . session ) {
553+ if ( earliestEvent ) {
554+ this . session . started = earliestEvent ;
555+ }
552556 this . _updateUserActivity ( activityTime ) ;
553557 this . _updateSessionActivity ( activityTime ) ;
554558 this . _maybeSaveSession ( ) ;
@@ -1222,35 +1226,14 @@ export class ReplayContainer implements ReplayContainerInterface {
12221226 return ;
12231227 }
12241228
1225- const start = this . session . started ;
1226- const now = Date . now ( ) ;
1227- const duration = now - start ;
1228-
12291229 // A flush is about to happen, cancel any queued flushes
12301230 this . _debouncedFlush . cancel ( ) ;
12311231
1232- // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1233- // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1234- const tooShort = duration < this . _options . minReplayDuration ;
1235- const tooLong = duration > this . _options . maxReplayDuration + 5_000 ;
1236- if ( tooShort || tooLong ) {
1237- DEBUG_BUILD &&
1238- logger . info (
1239- `Session duration (${ Math . floor ( duration / 1000 ) } s) is too ${
1240- tooShort ? 'short' : 'long'
1241- } , not sending replay.`,
1242- ) ;
1243-
1244- if ( tooShort ) {
1245- this . _debouncedFlush ( ) ;
1246- }
1232+ const isValidDuration = this . _checkReplayDurationDuringFlush ( ) ;
12471233
1248- // XXX: disregard durations for buffer mode for debug purposes
1249- if ( this . recordingMode !== 'buffer' ) {
1250- return ;
1251- } else {
1252- setTag ( `replay.${ tooShort ? 'tooShort' : 'tooLong' } ` , true ) ;
1253- }
1234+ // XXX: disregard durations for buffer mode for debug purposes
1235+ if ( ! isValidDuration && this . recordingMode !== 'buffer' ) {
1236+ return ;
12541237 }
12551238
12561239 const eventBuffer = this . eventBuffer ;
@@ -1285,6 +1268,52 @@ export class ReplayContainer implements ReplayContainerInterface {
12851268 }
12861269 } ;
12871270
1271+ /**
1272+ * Checks to see if replay duration is within bounds during a flush. If it is
1273+ * too short, will queue up a new flush to prevent short replays.
1274+ *
1275+ * Returns true if duration is ok, false otherwise
1276+ */
1277+ private _checkReplayDurationDuringFlush ( ) : boolean {
1278+ if ( ! this . session ) {
1279+ return false ;
1280+ }
1281+
1282+ const earliestTimestampFromBuffer = this . eventBuffer && this . eventBuffer . getEarliestTimestamp ( ) ;
1283+ const start =
1284+ this . recordingMode === 'buffer' && earliestTimestampFromBuffer
1285+ ? earliestTimestampFromBuffer
1286+ : this . session . started ;
1287+ const now = Date . now ( ) ;
1288+ const duration = now - start ;
1289+
1290+ // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1291+ // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1292+ const tooShort = duration < this . _options . minReplayDuration ;
1293+ const tooLong = duration > this . _options . maxReplayDuration + 5_000 ;
1294+ if ( tooShort || tooLong ) {
1295+ DEBUG_BUILD &&
1296+ logger . info (
1297+ `Session duration (${ Math . floor ( duration / 1000 ) } s) is too ${
1298+ tooShort ? 'short' : 'long'
1299+ } , not sending replay.`,
1300+ ) ;
1301+
1302+ if ( tooShort ) {
1303+ this . _debouncedFlush ( ) ;
1304+ }
1305+
1306+ // XXX: disregard durations for buffer mode for debug purposes
1307+ if ( this . recordingMode === 'buffer' ) {
1308+ setTag ( `replay.${ tooShort ? 'tooShort' : 'tooLong' } ` , true ) ;
1309+ }
1310+
1311+ return false ;
1312+ }
1313+
1314+ return true ;
1315+ }
1316+
12881317 /** Save the session, if it is sticky */
12891318 private _maybeSaveSession ( ) : void {
12901319 if ( this . session && this . _options . stickySession ) {
0 commit comments