@@ -59,6 +59,7 @@ export class WKPage implements PageDelegate {
5959 this . _page = new Page ( this , browserContext ) ;
6060 this . _workers = new WKWorkers ( this . _page ) ;
6161 this . _session = undefined as any as WKSession ;
62+ this . _page . on ( Events . Page . FrameDetached , frame => this . _removeContextsForFrame ( frame , false ) ) ;
6263 }
6364
6465 private async _initializePageProxySession ( ) {
@@ -248,13 +249,8 @@ export class WKPage implements PageDelegate {
248249
249250 private _onFrameNavigated ( framePayload : Protocol . Page . Frame , initial : boolean ) {
250251 const frame = this . _page . _frameManager . frame ( framePayload . id ) ;
251- for ( const [ contextId , context ] of this . _contextIdToContext ) {
252- if ( context . frame === frame ) {
253- ( context . _delegate as WKExecutionContext ) . _dispose ( ) ;
254- this . _contextIdToContext . delete ( contextId ) ;
255- frame . _contextDestroyed ( context ) ;
256- }
257- }
252+ assert ( frame ) ;
253+ this . _removeContextsForFrame ( frame ! , true ) ;
258254 if ( ! framePayload . parentId )
259255 this . _workers . clear ( ) ;
260256 this . _page . _frameManager . frameCommittedNewDocumentNavigation ( framePayload . id , framePayload . url , framePayload . name || '' , framePayload . loaderId , initial ) ;
@@ -268,6 +264,17 @@ export class WKPage implements PageDelegate {
268264 this . _page . _frameManager . frameDetached ( frameId ) ;
269265 }
270266
267+ private _removeContextsForFrame ( frame : frames . Frame , notifyFrame : boolean ) {
268+ for ( const [ contextId , context ] of this . _contextIdToContext ) {
269+ if ( context . frame === frame ) {
270+ ( context . _delegate as WKExecutionContext ) . _dispose ( ) ;
271+ this . _contextIdToContext . delete ( contextId ) ;
272+ if ( notifyFrame )
273+ frame . _contextDestroyed ( context ) ;
274+ }
275+ }
276+ }
277+
271278 private _onExecutionContextCreated ( contextPayload : Protocol . Runtime . ExecutionContextDescription ) {
272279 if ( this . _contextIdToContext . has ( contextPayload . id ) )
273280 return ;
0 commit comments