File tree Expand file tree Collapse file tree 2 files changed +20
-30
lines changed
packages/open-next/src/core/routing Expand file tree Collapse file tree 2 files changed +20
-30
lines changed Original file line number Diff line number Diff line change @@ -97,18 +97,7 @@ export async function handleMiddleware(
9797 url,
9898 body : convertBodyToReadableStream ( internalEvent . method , internalEvent . body ) ,
9999 } as unknown as Request ) ;
100-
101100 const statusCode = result . status ;
102- // We want to normalize the Location header if its a redirect response. Headers.has() doesn't care about the case of the header name.
103- if ( REDIRECTS . has ( statusCode ) && result . headers . has ( "Location" ) ) {
104- result . headers . set (
105- "Location" ,
106- normalizeLocationHeader (
107- result . headers . get ( "Location" ) as string ,
108- internalEvent . url ,
109- ) ,
110- ) ;
111- }
112101
113102 /* Apply override headers from middleware
114103 NextResponse.next({
@@ -146,6 +135,11 @@ export async function handleMiddleware(
146135 resHeaders [ key ] = resHeaders [ key ]
147136 ? [ ...resHeaders [ key ] , value ]
148137 : [ value ] ;
138+ } else if (
139+ REDIRECTS . has ( statusCode ) &&
140+ key . toLowerCase ( ) === "location"
141+ ) {
142+ resHeaders [ key ] = normalizeLocationHeader ( value , internalEvent . url ) ;
149143 } else {
150144 resHeaders [ key ] = value ;
151145 }
Original file line number Diff line number Diff line change @@ -452,26 +452,22 @@ export function normalizeLocationHeader(
452452 location : string ,
453453 baseUrl : string ,
454454) : string {
455- try {
456- const locationUrl = new URL ( location ) ;
457- const origin = new URL ( baseUrl ) . origin ;
458-
459- // Encode the search parameters to ensure they are valid according to RFC
460- const encodedSearch = locationUrl . searchParams . toString ( )
461- ? `?${ locationUrl . searchParams . toString ( ) } `
462- : "" ;
463- const href =
464- locationUrl . origin +
465- locationUrl . pathname +
466- encodedSearch +
467- locationUrl . hash ;
468- // The URL is relative if the origin is the same as the base URL's origin
469- if ( locationUrl . origin === origin ) {
470- return href . slice ( origin . length ) ;
471- }
472- return href ;
473- } catch {
455+ if ( ! URL . canParse ( location ) ) {
474456 // If the location is not a valid URL, return it as-is
475457 return location ;
476458 }
459+
460+ const locationUrl = new URL ( location ) ;
461+ const origin = new URL ( baseUrl ) . origin ;
462+
463+ // Encode the search parameters to ensure they are valid according to RFC
464+ const searchParams = locationUrl . searchParams . toString ( ) ;
465+ const encodedSearch = searchParams ? `?${ searchParams } ` : "" ;
466+ const href = `${ locationUrl . origin } ${ locationUrl . pathname } ${ encodedSearch } ${ locationUrl . hash } ` ;
467+
468+ // The URL is relative if the origin is the same as the base URL's origin
469+ if ( locationUrl . origin === origin ) {
470+ return href . slice ( origin . length ) ;
471+ }
472+ return href ;
477473}
You can’t perform that action at this time.
0 commit comments