@@ -833,7 +833,9 @@ export class ReplayContainer implements ReplayContainerInterface {
833
833
// Always increment segmentId regardless of outcome of sending replay
834
834
const segmentId = this . session . segmentId ++ ;
835
835
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.
837
839
setFlushState ( FlushState . START , {
838
840
events : this . eventBuffer . pendingEvents ,
839
841
replayId,
@@ -843,9 +845,13 @@ export class ReplayContainer implements ReplayContainerInterface {
843
845
timestamp : new Date ( ) . getTime ( ) ,
844
846
} ) ;
845
847
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.
847
851
this . _maybeSaveSession ( ) ;
848
852
853
+ // NOTE: Be mindful that nothing after this point (the first `await`)
854
+ // will run after when the page is unloaded.
849
855
const [ , , recordingData ] = await Promise . all ( promises ) ;
850
856
851
857
const sendReplayPromise = this . sendReplay ( {
0 commit comments