@@ -48,14 +48,17 @@ function PreviewContent({
48
48
method : 'HEAD' ,
49
49
cache : 'no-store' ,
50
50
signal : controller . signal ,
51
- headers : { 'Cache-Control' : 'no-cache' }
51
+ headers : { 'Cache-Control' : 'no-cache' } ,
52
52
} ) ;
53
-
53
+
54
54
clearTimeout ( timeoutId ) ;
55
-
55
+
56
56
// Service is ready if we get a successful response (not 404 or 5xx)
57
- const isReady = response . ok || ( response . status !== 404 && response . status < 500 ) ;
58
- console . log ( `Service check: ${ url } - Status: ${ response . status } - Ready: ${ isReady } ` ) ;
57
+ const isReady =
58
+ response . ok || ( response . status !== 404 && response . status < 500 ) ;
59
+ console . log (
60
+ `Service check: ${ url } - Status: ${ response . status } - Ready: ${ isReady } `
61
+ ) ;
59
62
return isReady ;
60
63
} catch ( error ) {
61
64
// Don't log abort errors (expected when timeout occurs)
@@ -76,19 +79,19 @@ function PreviewContent({
76
79
setServiceCheckAttempts ( 0 ) ;
77
80
setIsServiceReady ( false ) ;
78
81
setLoadingMessage ( 'Loading preview...' ) ;
79
-
82
+
80
83
// Try immediately first (don't wait for interval)
81
84
const initialReady = await checkServiceReady ( url ) ;
82
85
if ( initialReady ) {
83
86
console . log ( 'Frontend service is ready immediately!' ) ;
84
87
setIsServiceReady ( true ) ;
85
88
return ; // Exit early if service is ready immediately
86
89
}
87
-
90
+
88
91
// Progressive check intervals (check more frequently at first)
89
92
const checkIntervals = [ 500 , 1000 , 1000 , 1500 , 1500 ] ; // First few checks are faster
90
93
let checkIndex = 0 ;
91
-
94
+
92
95
// Set a fallback timer - show preview after 45 seconds no matter what
93
96
const fallbackTimer = setTimeout ( ( ) => {
94
97
console . log ( 'Fallback timer triggered - showing preview anyway' ) ;
@@ -98,17 +101,19 @@ function PreviewContent({
98
101
serviceCheckTimerRef . current = null ;
99
102
}
100
103
} , 45000 ) ;
101
-
104
+
102
105
const runServiceCheck = async ( ) => {
103
- setServiceCheckAttempts ( prev => prev + 1 ) ;
104
-
106
+ setServiceCheckAttempts ( ( prev ) => prev + 1 ) ;
107
+
105
108
// Update loading message with attempts
106
109
if ( serviceCheckAttempts > 3 ) {
107
- setLoadingMessage ( `Starting frontend service... (${ serviceCheckAttempts } /${ MAX_CHECK_ATTEMPTS } )` ) ;
110
+ setLoadingMessage (
111
+ `Starting frontend service... (${ serviceCheckAttempts } /${ MAX_CHECK_ATTEMPTS } )`
112
+ ) ;
108
113
}
109
-
114
+
110
115
const ready = await checkServiceReady ( url ) ;
111
-
116
+
112
117
if ( ready ) {
113
118
console . log ( 'Frontend service is ready!' ) ;
114
119
setIsServiceReady ( true ) ;
@@ -119,27 +124,32 @@ function PreviewContent({
119
124
}
120
125
} else if ( serviceCheckAttempts >= MAX_CHECK_ATTEMPTS ) {
121
126
// Service didn't become ready after max attempts
122
- console . log ( 'Max attempts reached. Service might still be initializing.' ) ;
123
- setLoadingMessage ( 'Preview might not be fully loaded. Click refresh to try again.' ) ;
124
-
127
+ console . log (
128
+ 'Max attempts reached. Service might still be initializing.'
129
+ ) ;
130
+ setLoadingMessage (
131
+ 'Preview might not be fully loaded. Click refresh to try again.'
132
+ ) ;
133
+
125
134
// Show the preview anyway after max attempts
126
135
setIsServiceReady ( true ) ;
127
136
clearTimeout ( fallbackTimer ) ;
128
-
137
+
129
138
if ( serviceCheckTimerRef . current ) {
130
139
clearInterval ( serviceCheckTimerRef . current ) ;
131
140
serviceCheckTimerRef . current = null ;
132
141
}
133
142
} else {
134
143
// Schedule next check with dynamic interval
135
- const nextInterval = checkIndex < checkIntervals . length
136
- ? checkIntervals [ checkIndex ++ ]
137
- : 2000 ; // Default to 2000ms after initial fast checks
138
-
144
+ const nextInterval =
145
+ checkIndex < checkIntervals . length
146
+ ? checkIntervals [ checkIndex ++ ]
147
+ : 2000 ; // Default to 2000ms after initial fast checks
148
+
139
149
setTimeout ( runServiceCheck , nextInterval ) ;
140
150
}
141
151
} ;
142
-
152
+
143
153
// Start the first check
144
154
setTimeout ( runServiceCheck , 500 ) ;
145
155
} ;
@@ -164,7 +174,7 @@ function PreviewContent({
164
174
}
165
175
166
176
lastProjectPathRef . current = projectPath ;
167
-
177
+
168
178
// Reset service ready state for new project
169
179
setIsServiceReady ( false ) ;
170
180
@@ -187,7 +197,7 @@ function PreviewContent({
187
197
console . log ( 'baseUrl:' , baseUrl ) ;
188
198
setBaseUrl ( baseUrl ) ;
189
199
setDisplayPath ( '/' ) ;
190
-
200
+
191
201
// Start checking if the service is ready
192
202
startServiceReadyCheck ( baseUrl ) ;
193
203
} catch ( error ) {
@@ -250,7 +260,7 @@ function PreviewContent({
250
260
setIsServiceReady ( false ) ;
251
261
startServiceReadyCheck ( baseUrl ) ;
252
262
}
253
-
263
+
254
264
const iframe = document . getElementById ( 'myIframe' ) as HTMLIFrameElement ;
255
265
if ( iframe ) {
256
266
const src = iframe . src ;
@@ -290,7 +300,9 @@ function PreviewContent({
290
300
size = "icon"
291
301
className = "h-6 w-6"
292
302
onClick = { goForward }
293
- disabled = { ! baseUrl || currentIndex >= history . length - 1 || ! isServiceReady }
303
+ disabled = {
304
+ ! baseUrl || currentIndex >= history . length - 1 || ! isServiceReady
305
+ }
294
306
>
295
307
< ChevronRight className = "h-4 w-4" />
296
308
</ Button >
@@ -379,11 +391,13 @@ function PreviewContent({
379
391
< div className = "flex flex-col items-center gap-2" >
380
392
< div className = "flex items-center gap-2" >
381
393
< div className = "animate-spin w-4 h-4 border-2 border-primary border-t-transparent rounded-full" > </ div >
382
- < p className = "text-sm text-muted-foreground" > { loadingMessage } </ p >
394
+ < p className = "text-sm text-muted-foreground" >
395
+ { loadingMessage }
396
+ </ p >
383
397
</ div >
384
398
{ serviceCheckAttempts > 5 && (
385
- < Button
386
- variant = "outline"
399
+ < Button
400
+ variant = "outline"
387
401
size = "sm"
388
402
onClick = { ( ) => {
389
403
if ( baseUrl ) {
@@ -414,4 +428,4 @@ export default function WebPreview() {
414
428
}
415
429
416
430
return < PreviewContent curProject = { curProject } getWebUrl = { getWebUrl } /> ;
417
- }
431
+ }
0 commit comments