@@ -12,12 +12,41 @@ const server = prerender({
1212 '--disable-dev-shm-usage' ,
1313 '--ignore-certificate-errors' ,
1414 '--allow-insecure-localhost'
15- ]
15+ ] ,
16+ pageLoadTimeout : 20000 ,
17+ waitAfterLastRequest : 500 ,
18+ pageDoneCheckInterval : 300
1619} ) ;
1720
1821process . env . CACHE_MAXSIZE = process . env . CACHE_MAXSIZE || 1000 ;
1922process . env . CACHE_TTL = process . env . CACHE_TTL || 43200 ;
2023
24+ // Block requests that typically hang and prevent page from finishing
25+ const BLOCKED_PATTERNS = [
26+ / g o o g l e - a n a l y t i c s \. c o m / ,
27+ / g o o g l e t a g m a n a g e r \. c o m / ,
28+ / f a c e b o o k \. n e t / ,
29+ / f a c e b o o k \. c o m \/ t r / ,
30+ / h o t j a r \. c o m / ,
31+ / i n t e r c o m \. i o / ,
32+ / c r i s p \. c h a t / ,
33+ / s e n t r y \. i o / ,
34+ / s e g m e n t \. c o m / ,
35+ / m i x p a n e l \. c o m / ,
36+ / a m p l i t u d e \. c o m / ,
37+ / c l a r i t y \. m s / ,
38+ / d o u b l e c l i c k \. n e t / ,
39+ / \. w o f f 2 ? ( \? | $ ) / ,
40+ / \. t t f ( \? | $ ) / ,
41+ / w s : \/ \/ / ,
42+ / w s s : \/ \/ / ,
43+ / \/ s o c k e t \. i o \/ / ,
44+ / \/ s o c k j s - n o d e \/ / ,
45+ / \/ h u b \? / , // SignalR
46+ / e v e n t s o u r c e / i,
47+ / l i v e r e l o a d / ,
48+ ] ;
49+
2150server . use ( {
2251 tabCreated : ( req , res , next ) => {
2352 const tab = req . prerender . tab ;
@@ -26,18 +55,64 @@ server.use({
2655 tab . Console . enable ( ) ;
2756 tab . Network . enable ( ) ;
2857
29- tab . Console . messageAdded ( ( params ) => {
30- console . log ( '🟡 Browser log:' , params . message . text ) ;
58+ const pendingRequests = new Map ( ) ;
59+
60+ // Use Fetch domain for request interception (replaces deprecated Network.setRequestInterception)
61+ tab . Fetch . enable ( { patterns : [ { requestStage : 'Request' } ] } ) ;
62+ tab . Fetch . requestPaused ( ( params ) => {
63+ const url = params . request . url ;
64+ const blocked = BLOCKED_PATTERNS . some ( p => p . test ( url ) ) ;
65+ if ( blocked ) {
66+ console . log ( '🚫 Blocked:' , url ) ;
67+ tab . Fetch . failRequest ( {
68+ requestId : params . requestId ,
69+ errorReason : 'Aborted'
70+ } ) ;
71+ } else {
72+ tab . Fetch . continueRequest ( { requestId : params . requestId } ) ;
73+ }
74+ } ) ;
75+
76+ tab . Network . requestWillBeSent ( ( params ) => {
77+ pendingRequests . set ( params . requestId , {
78+ url : params . request . url ,
79+ time : Date . now ( )
80+ } ) ;
81+ } ) ;
82+
83+ tab . Network . loadingFinished ( ( params ) => {
84+ pendingRequests . delete ( params . requestId ) ;
3185 } ) ;
3286
3387 tab . Network . loadingFailed ( ( params ) => {
34- console . log ( '🔴 Network Failed:' , params . errorText , params . url ) ;
88+ const info = pendingRequests . get ( params . requestId ) ;
89+ if ( info ) {
90+ console . log ( '🔴 Network Failed:' , params . errorText , info . url ) ;
91+ pendingRequests . delete ( params . requestId ) ;
92+ }
93+ } ) ;
94+
95+ tab . Console . messageAdded ( ( params ) => {
96+ console . log ( '🟡 Browser log:' , params . message . text ) ;
3597 } ) ;
3698
3799 tab . Runtime . enable ( ) ;
38100 tab . Runtime . exceptionThrown ( ( exception ) => {
39101 console . log ( '💥 JS Exception:' , exception . exceptionDetails . text ) ;
40102 } ) ;
103+
104+ // Log pending requests every 3s to identify what's hanging
105+ const interval = setInterval ( ( ) => {
106+ if ( pendingRequests . size > 0 ) {
107+ const now = Date . now ( ) ;
108+ console . log ( `⏳ Pending requests (${ pendingRequests . size } ):` ) ;
109+ pendingRequests . forEach ( ( { url, time } ) => {
110+ console . log ( ` - ${ ( ( now - time ) / 1000 ) . toFixed ( 1 ) } s ${ url } ` ) ;
111+ } ) ;
112+ }
113+ } , 3000 ) ;
114+
115+ req . prerender . on ( 'tabNavigated' , ( ) => clearInterval ( interval ) ) ;
41116 }
42117
43118 next ( ) ;
@@ -51,16 +126,13 @@ server.use({
51126 req . prerender . res . headers = {
52127 'content-type' : 'text/html; charset=utf-8' ,
53128 'content-length' : Buffer . byteLength ( body , 'utf8' ) ,
54- // 'cache-control': 'public, max-age=600'
55129 } ;
56130 }
57131 next ( ) ;
58132 }
59133} ) ;
60134
61-
62135server . use ( prerender . removeScriptTags ( ) ) ;
63-
64136server . use ( memoryCache ) ;
65137
66138console . log ( 'Prerender on Node 24 is starting...' ) ;
0 commit comments