@@ -170,10 +170,16 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
170170 this . _state . recording = true ;
171171 this . _state . callIds . clear ( ) ;
172172
173+ // - Browser context network trace is shared across chunks as it contains resources
174+ // used to serve page snapshots, so make a copy with the new name.
175+ // - APIRequestContext network traces are chunk-specific, always start from scratch.
176+ const preserveNetworkResources = this . _context instanceof BrowserContext ;
173177 if ( options . name && options . name !== this . _state . traceName )
174- this . _changeTraceName ( this . _state , options . name ) ;
178+ this . _changeTraceName ( this . _state , options . name , preserveNetworkResources ) ;
175179 else
176180 this . _allocateNewTraceFile ( this . _state ) ;
181+ if ( ! preserveNetworkResources )
182+ this . _fs . writeFile ( this . _state . networkFile , '' ) ;
177183
178184 this . _fs . mkdir ( path . dirname ( this . _state . traceFile ) ) ;
179185 const event : trace . TraceEvent = {
@@ -267,14 +273,14 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
267273 state . traceFile = path . join ( state . tracesDir , `${ state . traceName } ${ suffix } .trace` ) ;
268274 }
269275
270- private _changeTraceName ( state : RecordingState , name : string ) {
276+ private _changeTraceName ( state : RecordingState , name : string , preserveNetworkResources : boolean ) {
271277 state . traceName = name ;
272278 state . chunkOrdinal = 0 ; // Reset ordinal for the new name.
273279 this . _allocateNewTraceFile ( state ) ;
274280
275- // Network file survives across chunks, so make a copy with the new name.
276281 const newNetworkFile = path . join ( state . tracesDir , name + '.network' ) ;
277- this . _fs . copyFile ( state . networkFile , newNetworkFile ) ;
282+ if ( preserveNetworkResources )
283+ this . _fs . copyFile ( state . networkFile , newNetworkFile ) ;
278284 state . networkFile = newNetworkFile ;
279285 }
280286
0 commit comments