@@ -9,8 +9,9 @@ const globalCache = new NodeCache({ stdTTL: 3600, checkperiod: 600 });
99const MAX_RESOURCE_SIZE = 15 * ( 1024 ** 2 ) ; // 15MB
1010var ALLOWED_RESOURCES = [ 'document' , 'stylesheet' , 'image' , 'media' , 'font' , 'other' ] ;
1111const ALLOWED_STATUSES = [ 200 , 201 ] ;
12- const REQUEST_TIMEOUT = 1800000 ;
12+ const REQUEST_TIMEOUT = 180000 ;
1313const MIN_VIEWPORT_HEIGHT = 1080 ;
14+ const MAX_WAIT_FOR_REQUEST_CALL = 30000 ;
1415
1516const normalizeSameSite = ( value ) => {
1617 if ( ! value ) return 'Lax' ;
@@ -312,6 +313,8 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
312313 }
313314 }
314315
316+ const pendingRequests = new Set < string > ( ) ;
317+
315318 // Use route to intercept network requests and discover resources
316319 await page . route ( '**/*' , async ( route , request ) => {
317320 const requestUrl = request . url ( )
@@ -370,8 +373,14 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
370373 body = globalCache . get ( requestUrl ) . body ;
371374 } else {
372375 ctx . log . debug ( `Resource not found in cache or global cache ${ requestUrl } fetching from server` ) ;
376+ if ( ctx . build . checkPendingRequests ) {
377+ pendingRequests . add ( requestUrl ) ;
378+ }
373379 response = await page . request . fetch ( request , requestOptions ) ;
374380 body = await response . body ( ) ;
381+ if ( ctx . build . checkPendingRequests ) {
382+ pendingRequests . delete ( requestUrl ) ;
383+ }
375384 }
376385
377386 // handle response
@@ -399,15 +408,26 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
399408
400409 let responseOfRetry , bodyOfRetry
401410 ctx . log . debug ( `Resource had a disallowed status ${ requestUrl } fetching from server again` ) ;
411+ if ( ctx . build . checkPendingRequests ) {
412+ pendingRequests . add ( requestUrl ) ;
413+ }
402414 responseOfRetry = await page . request . fetch ( request , requestOptions ) ;
403415 bodyOfRetry = await responseOfRetry . body ( ) ;
404-
416+ if ( ctx . build . checkPendingRequests ) {
417+ pendingRequests . delete ( requestUrl ) ;
418+ }
405419 if ( responseOfRetry && responseOfRetry . status ( ) && ALLOWED_STATUSES . includes ( responseOfRetry . status ( ) ) ) {
406420 ctx . log . debug ( `Handling request after retry ${ requestUrl } \n - content-type ${ responseOfRetry . headers ( ) [ 'content-type' ] } ` ) ;
407421 cache [ requestUrl ] = {
408422 body : bodyOfRetry . toString ( 'base64' ) ,
409423 type : responseOfRetry . headers ( ) [ 'content-type' ]
410424 }
425+ if ( ctx . config . useGlobalCache ) {
426+ globalCache . set ( requestUrl , {
427+ body : bodyOfRetry . toString ( 'base64' ) ,
428+ type : responseOfRetry . headers ( ) [ 'content-type' ]
429+ } ) ;
430+ }
411431 route . fulfill ( {
412432 status : responseOfRetry . status ( ) ,
413433 headers : responseOfRetry . headers ( ) ,
@@ -458,6 +478,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
458478 }
459479 }
460480
481+
461482 // Continue the request with the fetched response
462483 route . fulfill ( {
463484 status : response . status ( ) ,
@@ -663,6 +684,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
663684 } catch ( error ) {
664685 ctx . log . debug ( `Network idle failed due to ${ error } ` ) ;
665686 }
687+
666688
667689
668690 if ( ctx . config . allowedAssets && ctx . config . allowedAssets . length ) {
@@ -852,6 +874,28 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
852874 ctx . log . debug ( `Processed options: ${ JSON . stringify ( processedOptions ) } ` ) ;
853875 }
854876
877+ // Wait for pending requests to complete
878+ const checkPending = async ( ) => {
879+ let startTime = Date . now ( ) ;
880+ ctx . log . debug ( `${ pendingRequests . size } Pending requests before wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
881+ while ( pendingRequests . size > 0 ) {
882+ const elapsedTime = Date . now ( ) - startTime ;
883+ if ( elapsedTime >= MAX_WAIT_FOR_REQUEST_CALL ) {
884+ ctx . log . debug ( `Timeout reached (${ MAX_WAIT_FOR_REQUEST_CALL / 1000 } s). Stopping wait for pending requests.` ) ;
885+ ctx . log . debug ( `${ pendingRequests . size } Pending requests after wait for ${ snapshot . name } : ${ Array . from ( pendingRequests ) } ` ) ;
886+ break ;
887+ }
888+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
889+ }
890+ if ( pendingRequests . size === 0 ) {
891+ ctx . log . debug ( `No pending requests for ${ snapshot . name } .` ) ;
892+ }
893+ } ;
894+
895+ if ( ctx . build . checkPendingRequests ) {
896+ await checkPending ( ) ;
897+ }
898+
855899
856900 let hasBrowserErrors = false ;
857901 for ( let browser in discoveryErrors . browsers ) {
0 commit comments