@@ -320,11 +320,21 @@ export function fetchScriptsFromHtml (
320320
321321 const fiberScriptTasks : fiberTasks = app . isPrefetch || app . fiber ? [ ] : null
322322
323+ const startIdentifier = app . mountIdentifier
324+
323325 if ( fetchScriptPromise . length ) {
324326 promiseStream < string > ( fetchScriptPromise , ( res : { data : string , index : number } ) => {
327+ const address = fetchScriptPromiseInfo [ res . index ] [ 0 ]
328+ const scriptInfo = fetchScriptPromiseInfo [ res . index ] [ 1 ]
329+ const currIdentifier = app . mountIdentifier
330+ if ( startIdentifier !== currIdentifier ) {
331+ scriptInfo . code = res . data
332+ // app has been unmounted, old sandbox is not valid anymore. should not execute script
333+ throw new Error ( 'app has been unmounted, abort loading script' )
334+ }
325335 injectFiberTask ( fiberScriptTasks , ( ) => fetchScriptSuccess (
326- fetchScriptPromiseInfo [ res . index ] [ 0 ] ,
327- fetchScriptPromiseInfo [ res . index ] [ 1 ] ,
336+ address ,
337+ scriptInfo ,
328338 res . data ,
329339 app ,
330340 ) )
@@ -578,8 +588,14 @@ export function runDynamicRemoteScript (
578588 if ( scriptInfo . code || isTypeModule ( app , scriptInfo ) ) {
579589 defer ( runDynamicScript )
580590 } else {
591+ const startIdentifier = app . mountIdentifier
581592 fetchSource ( address , app . name ) . then ( ( code : string ) => {
593+ const currIdentifier = app . mountIdentifier
582594 scriptInfo . code = code
595+ if ( startIdentifier !== currIdentifier ) {
596+ // app has been unmounted, old sandbox is not valid anymore. should not execute script
597+ throw new Error ( 'app has been unmounted, abort loading script' )
598+ }
583599 runDynamicScript ( )
584600 } ) . catch ( ( err ) => {
585601 logError ( err , app . name )
0 commit comments