@@ -846,7 +846,9 @@ export class ReplayContainer implements ReplayContainerInterface {
846
846
// Always increment segmentId regardless of outcome of sending replay
847
847
const segmentId = this . session . segmentId ++ ;
848
848
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.
850
852
setFlushState ( FlushState . START , {
851
853
events : this . eventBuffer . pendingEvents ,
852
854
replayId,
@@ -856,9 +858,13 @@ export class ReplayContainer implements ReplayContainerInterface {
856
858
timestamp : new Date ( ) . getTime ( ) ,
857
859
} ) ;
858
860
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.
860
864
this . _maybeSaveSession ( ) ;
861
865
866
+ // NOTE: Be mindful that nothing after this point (the first `await`)
867
+ // will run after when the page is unloaded.
862
868
const [ , , recordingData ] = await Promise . all ( promises ) ;
863
869
864
870
const sendReplayPromise = this . sendReplay ( {
0 commit comments