@@ -11,7 +11,7 @@ var ALLOWED_RESOURCES = ['document', 'stylesheet', 'image', 'media', 'font', 'ot
1111const ALLOWED_STATUSES = [ 200 , 201 ] ;
1212const REQUEST_TIMEOUT = 180000 ;
1313const MIN_VIEWPORT_HEIGHT = 1080 ;
14- const MAX_WAIT_FOR_REQUEST_CALL = 60000 ;
14+ const MAX_WAIT_FOR_REQUEST_CALL = 30000 ;
1515
1616export async function prepareSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
1717 let processedOptions : Record < string , any > = { } ;
@@ -360,10 +360,14 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
360360 body = globalCache . get ( requestUrl ) . body ;
361361 } else {
362362 ctx . log . debug ( `Resource not found in cache or global cache ${ requestUrl } fetching from server` ) ;
363- pendingRequests . add ( requestUrl ) ;
363+ if ( ctx . build . checkPendingRequests ) {
364+ pendingRequests . add ( requestUrl ) ;
365+ }
364366 response = await page . request . fetch ( request , requestOptions ) ;
365367 body = await response . body ( ) ;
366- pendingRequests . delete ( requestUrl ) ;
368+ if ( ctx . build . checkPendingRequests ) {
369+ pendingRequests . delete ( requestUrl ) ;
370+ }
367371 }
368372
369373 // handle response
@@ -391,10 +395,14 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
391395
392396 let responseOfRetry , bodyOfRetry
393397 ctx . log . debug ( `Resource had a disallowed status ${ requestUrl } fetching from server again` ) ;
394- pendingRequests . add ( requestUrl ) ;
398+ if ( ctx . build . checkPendingRequests ) {
399+ pendingRequests . add ( requestUrl ) ;
400+ }
395401 responseOfRetry = await page . request . fetch ( request , requestOptions ) ;
396402 bodyOfRetry = await responseOfRetry . body ( ) ;
397- pendingRequests . delete ( requestUrl ) ;
403+ if ( ctx . build . checkPendingRequests ) {
404+ pendingRequests . delete ( requestUrl ) ;
405+ }
398406 if ( responseOfRetry && responseOfRetry . status ( ) && ALLOWED_STATUSES . includes ( responseOfRetry . status ( ) ) ) {
399407 ctx . log . debug ( `Handling request after retry ${ requestUrl } \n - content-type ${ responseOfRetry . headers ( ) [ 'content-type' ] } ` ) ;
400408 cache [ requestUrl ] = {
@@ -663,27 +671,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
663671 } catch ( error ) {
664672 ctx . log . debug ( `Network idle failed due to ${ error } ` ) ;
665673 }
666-
667- // Wait for pending requests to complete
668- const checkPending = async ( ) => {
669- const startTime = Date . now ( ) ;
670- ctx . log . debug ( `${ pendingRequests . size } Pending requests before wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
671- while ( pendingRequests . size > 0 ) {
672- const elapsedTime = Date . now ( ) - startTime ;
673- if ( elapsedTime >= MAX_WAIT_FOR_REQUEST_CALL ) {
674- ctx . log . debug ( `Timeout reached (${ MAX_WAIT_FOR_REQUEST_CALL / 1000 } s). Stopping wait for pending requests.` ) ;
675- ctx . log . debug ( `${ pendingRequests . size } Pending requests after wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
676-
677- break ;
678- }
679- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
680- }
681- if ( pendingRequests . size === 0 ) {
682- ctx . log . debug ( `No pending requests for ${ snapshot . name } .` ) ;
683- }
684- } ;
685-
686- await checkPending ( ) ;
687674
688675
689676
@@ -874,6 +861,28 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
874861 ctx . log . debug ( `Processed options: ${ JSON . stringify ( processedOptions ) } ` ) ;
875862 }
876863
864+ // Wait for pending requests to complete
865+ const checkPending = async ( ) => {
866+ let startTime = Date . now ( ) ;
867+ ctx . log . debug ( `${ pendingRequests . size } Pending requests before wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
868+ while ( pendingRequests . size > 0 ) {
869+ const elapsedTime = Date . now ( ) - startTime ;
870+ if ( elapsedTime >= MAX_WAIT_FOR_REQUEST_CALL ) {
871+ ctx . log . debug ( `Timeout reached (${ MAX_WAIT_FOR_REQUEST_CALL / 1000 } s). Stopping wait for pending requests.` ) ;
872+ ctx . log . debug ( `${ pendingRequests . size } Pending requests after wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
873+ break ;
874+ }
875+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
876+ }
877+ if ( pendingRequests . size === 0 ) {
878+ ctx . log . debug ( `No pending requests for ${ snapshot . name } .` ) ;
879+ }
880+ } ;
881+
882+ if ( ctx . build . checkPendingRequests ) {
883+ await checkPending ( ) ;
884+ }
885+
877886
878887 let hasBrowserErrors = false ;
879888 for ( let browser in discoveryErrors . browsers ) {
0 commit comments