@@ -833,7 +833,9 @@ export class ReplayContainer implements ReplayContainerInterface {
833833 // Always increment segmentId regardless of outcome of sending replay
834834 const segmentId = this . session . segmentId ++ ;
835835
836- // Write to local storage before flushing, in case flush request never starts
836+ // Write to local storage before flushing, in case flush request never starts.
837+ // Ensure that this happens before *any* `await` happens, otherwise we
838+ // will lose data.
837839 setFlushState ( FlushState . START , {
838840 events : this . eventBuffer . pendingEvents ,
839841 replayId,
@@ -843,9 +845,13 @@ export class ReplayContainer implements ReplayContainerInterface {
843845 timestamp : new Date ( ) . getTime ( ) ,
844846 } ) ;
845847
846- // Save session (new segment id) after we save flush data assuming
848+ // Save session (new segment id) after we save flush data assuming either
849+ // 1) request succeeds or 2) it fails or never happens, in which case we
850+ // need to retry this segment.
847851 this . _maybeSaveSession ( ) ;
848852
853+ // NOTE: Be mindful that nothing after this point (the first `await`)
854+ // will run after when the page is unloaded.
849855 const [ , , recordingData ] = await Promise . all ( promises ) ;
850856
851857 const sendReplayPromise = this . sendReplay ( {
0 commit comments