@@ -122,10 +122,14 @@ const formatEdgeFunctionError = (errorBuffer, acceptsHtml) => {
122
122
} )
123
123
}
124
124
125
- function isInternal ( url ?: string ) : boolean {
125
+ function isInternalFunctions ( url ?: string ) : boolean {
126
126
return url ?. startsWith ( '/.netlify/' ) ?? false
127
127
}
128
128
129
+ function isInternal ( url ?: string ) : boolean {
130
+ return url ?. startsWith ( '/' ) ?? false
131
+ }
132
+
129
133
function isFunction ( functionsPort : boolean | number | undefined , url : string ) {
130
134
return functionsPort && url . match ( DEFAULT_FUNCTION_URL_EXPRESSION )
131
135
}
@@ -202,7 +206,7 @@ const handleAddonUrl = function ({ addonUrl, req, res }) {
202
206
203
207
// @ts -expect-error TS(7006) FIXME: Parameter 'match' implicitly has an 'any' type.
204
208
const isRedirect = function ( match ) {
205
- return match . status && match . status >= 300 && match . status <= 400
209
+ return match . status && match . status >= 300 && match . status < 400
206
210
}
207
211
208
212
// @ts -expect-error TS(7006) FIXME: Parameter 'publicFolder' implicitly has an 'any' t... Remove this comment to see the full error message
@@ -417,8 +421,8 @@ const serveRedirect = async function ({
417
421
const ct = req . headers [ 'content-type' ] ? contentType . parse ( req ) . type : ''
418
422
if (
419
423
req . method === 'POST' &&
420
- ! isInternal ( req . url ) &&
421
- ! isInternal ( destURL ) &&
424
+ ! isInternalFunctions ( req . url ) &&
425
+ ! isInternalFunctions ( destURL ) &&
422
426
( ct . endsWith ( '/x-www-form-urlencoded' ) || ct === 'multipart/form-data' )
423
427
) {
424
428
return proxy . web ( req , res , { target : options . functionsServer } )
@@ -452,9 +456,11 @@ const serveRedirect = async function ({
452
456
453
457
return proxy . web ( req , res , { headers : functionHeaders , target : options . functionsServer } )
454
458
}
459
+
455
460
if ( isImageRequest ( req ) ) {
456
461
return imageProxy ( req , res )
457
462
}
463
+
458
464
const addonUrl = getAddonUrl ( options . addonsUrls , req )
459
465
if ( addonUrl ) {
460
466
return handleAddonUrl ( { req, res, addonUrl } )
@@ -519,11 +525,14 @@ const initializeProxy = async function ({
519
525
}
520
526
} )
521
527
522
- proxy . on ( 'error' , ( _ , req , res ) => {
523
- // @ts -expect-error TS(2339) FIXME: Property 'writeHead' does not exist on type 'Socke... Remove this comment to see the full error message
524
- res . writeHead ( 500 , {
525
- 'Content-Type' : 'text/plain' ,
526
- } )
528
+ proxy . on ( 'error' , ( err , req , res ) => {
529
+ console . error ( 'Got error from proxy' , err ) ;
530
+
531
+ if ( res instanceof http . ServerResponse ) {
532
+ res . writeHead ( 500 , {
533
+ 'Content-Type' : 'text/plain' ,
534
+ } )
535
+ }
527
536
528
537
const message = isEdgeFunctionsRequest ( req )
529
538
? 'There was an error with an Edge Function. Please check the terminal for more details.'
@@ -596,24 +605,15 @@ const initializeProxy = async function ({
596
605
}
597
606
}
598
607
599
- // @ts -expect-error TS(2339) FIXME: Property 'proxyOptions' does not exist on type 'In... Remove this comment to see the full error message
600
- if ( req . proxyOptions . staticFile && isRedirect ( { status : proxyRes . statusCode } ) && proxyRes . headers . location ) {
601
- req . url = proxyRes . headers . location
602
- return serveRedirect ( {
603
- // We don't want to match functions at this point because any redirects
604
- // to functions will have already been processed, so we don't supply a
605
- // functions registry to `serveRedirect`.
606
- functionsRegistry : null ,
607
- req,
608
- res,
609
- proxy : handlers ,
610
- imageProxy,
611
- match : null ,
612
- // @ts -expect-error TS(2339) FIXME: Property 'proxyOptions' does not exist on type 'In... Remove this comment to see the full error message
613
- options : req . proxyOptions ,
614
- siteInfo,
615
- env,
608
+ if ( isRedirect ( { status : proxyRes . statusCode } ) && proxyRes . headers . location ) {
609
+ // ssg wants to redirect response, e.g. gatsby wants to change page to page/, follow it in proxy server as well
610
+ const destURL = proxyRes . headers . location
611
+ res . writeHead ( proxyRes . statusCode ?? 200 , {
612
+ Location : destURL ,
613
+ 'Cache-Control' : 'no-cache' ,
616
614
} )
615
+ res . end ( `Redirecting to ${ destURL } ` )
616
+ return
617
617
}
618
618
619
619
// @ts -expect-error TS(7034) FIXME: Variable 'responseData' implicitly has type 'any[]... Remove this comment to see the full error message
@@ -838,7 +838,7 @@ const onRequest = async (
838
838
hasFormSubmissionHandler &&
839
839
functionsServer &&
840
840
req . method === 'POST' &&
841
- ! isInternal ( req . url ) &&
841
+ ! isInternalFunctions ( req . url ) &&
842
842
( ct . endsWith ( '/x-www-form-urlencoded' ) || ct === 'multipart/form-data' )
843
843
) {
844
844
return proxy . web ( req , res , { target : functionsServer } )
0 commit comments