@@ -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 } )
@@ -428,14 +432,13 @@ const serveRedirect = async function ({
428
432
const matchingFunction =
429
433
functionsRegistry &&
430
434
( await functionsRegistry . getFunctionForURLPath ( destURL , req . method , ( ) => Boolean ( destStaticFile ) ) )
431
- let statusValue
432
435
if (
433
436
match . force ||
434
437
( ! staticFile && ( ( ! options . framework && destStaticFile ) || isInternal ( destURL ) || matchingFunction ) )
435
438
) {
439
+ // 3xx redirects parsed above, here are 2xx meaning just override the url of proxying page and use the status
440
+ // which comes from that url
436
441
req . url = destStaticFile ? destStaticFile + dest . search : destURL
437
- const { status } = match
438
- statusValue = status
439
442
console . log ( `${ NETLIFYDEVLOG } Rewrote URL to` , req . url )
440
443
}
441
444
@@ -452,15 +455,17 @@ const serveRedirect = async function ({
452
455
453
456
return proxy . web ( req , res , { headers : functionHeaders , target : options . functionsServer } )
454
457
}
458
+
455
459
if ( isImageRequest ( req ) ) {
456
460
return imageProxy ( req , res )
457
461
}
462
+
458
463
const addonUrl = getAddonUrl ( options . addonsUrls , req )
459
464
if ( addonUrl ) {
460
465
return handleAddonUrl ( { req, res, addonUrl } )
461
466
}
462
467
463
- return proxy . web ( req , res , { ... options , status : statusValue } )
468
+ return proxy . web ( req , res , options )
464
469
}
465
470
466
471
return proxy . web ( req , res , options )
@@ -519,11 +524,14 @@ const initializeProxy = async function ({
519
524
}
520
525
} )
521
526
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
- } )
527
+ proxy . on ( 'error' , ( err , req , res ) => {
528
+ console . error ( 'Got error from proxy' , err )
529
+
530
+ if ( res instanceof http . ServerResponse ) {
531
+ res . writeHead ( 500 , {
532
+ 'Content-Type' : 'text/plain' ,
533
+ } )
534
+ }
527
535
528
536
const message = isEdgeFunctionsRequest ( req )
529
537
? 'There was an error with an Edge Function. Please check the terminal for more details.'
@@ -596,8 +604,7 @@ const initializeProxy = async function ({
596
604
}
597
605
}
598
606
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 ) {
607
+ if ( isRedirect ( { status : proxyRes . statusCode } ) && proxyRes . headers . location ) {
601
608
req . url = proxyRes . headers . location
602
609
return serveRedirect ( {
603
610
// We don't want to match functions at this point because any redirects
@@ -838,7 +845,7 @@ const onRequest = async (
838
845
hasFormSubmissionHandler &&
839
846
functionsServer &&
840
847
req . method === 'POST' &&
841
- ! isInternal ( req . url ) &&
848
+ ! isInternalFunctions ( req . url ) &&
842
849
( ct . endsWith ( '/x-www-form-urlencoded' ) || ct === 'multipart/form-data' )
843
850
) {
844
851
return proxy . web ( req , res , { target : functionsServer } )
0 commit comments