@@ -846,7 +846,9 @@ export class ReplayContainer implements ReplayContainerInterface {
846846 // Always increment segmentId regardless of outcome of sending replay
847847 const segmentId = this . session . segmentId ++ ;
848848
849- // Write to local storage before flushing, in case flush request never starts
849+ // Write to local storage before flushing, in case flush request never starts.
850+ // Ensure that this happens before *any* `await` happens, otherwise we
851+ // will lose data.
850852 setFlushState ( FlushState . START , {
851853 events : this . eventBuffer . pendingEvents ,
852854 replayId,
@@ -856,9 +858,13 @@ export class ReplayContainer implements ReplayContainerInterface {
856858 timestamp : new Date ( ) . getTime ( ) ,
857859 } ) ;
858860
859- // Save session (new segment id) after we save flush data assuming
861+ // Save session (new segment id) after we save flush data assuming either
862+ // 1) request succeeds or 2) it fails or never happens, in which case we
863+ // need to retry this segment.
860864 this . _maybeSaveSession ( ) ;
861865
866+ // NOTE: Be mindful that nothing after this point (the first `await`)
867+ // will run after when the page is unloaded.
862868 const [ , , recordingData ] = await Promise . all ( promises ) ;
863869
864870 const sendReplayPromise = this . sendReplay ( {
0 commit comments