File tree Expand file tree Collapse file tree 8 files changed +25
-0
lines changed Expand file tree Collapse file tree 8 files changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @opennextjs/aws " : patch
3
+ ---
4
+
5
+ fix: Propagate the status code in middleware rewrites
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import {
20
20
import { constructNextUrl } from "../core/routing/util" ;
21
21
import routingHandler , {
22
22
INTERNAL_EVENT_REQUEST_ID ,
23
+ INTERNAL_HEADER_REWRITE_STATUS_CODE ,
23
24
INTERNAL_HEADER_INITIAL_URL ,
24
25
INTERNAL_HEADER_RESOLVED_ROUTES ,
25
26
} from "../core/routingHandler" ;
@@ -86,6 +87,9 @@ const defaultHandler = async (
86
87
result . resolvedRoutes ,
87
88
) ,
88
89
[ INTERNAL_EVENT_REQUEST_ID ] : requestId ,
90
+ [ INTERNAL_HEADER_REWRITE_STATUS_CODE ] : String (
91
+ result . rewriteStatusCode ,
92
+ ) ,
89
93
} ,
90
94
} ,
91
95
isExternalRewrite : result . isExternalRewrite ,
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import {
23
23
} from "./routing/util" ;
24
24
import routingHandler , {
25
25
INTERNAL_EVENT_REQUEST_ID ,
26
+ INTERNAL_HEADER_REWRITE_STATUS_CODE ,
26
27
INTERNAL_HEADER_INITIAL_URL ,
27
28
INTERNAL_HEADER_RESOLVED_ROUTES ,
28
29
MIDDLEWARE_HEADER_PREFIX ,
@@ -69,6 +70,9 @@ export async function openNextHandler(
69
70
resolvedRoutes : initialHeaders [ INTERNAL_HEADER_RESOLVED_ROUTES ]
70
71
? JSON . parse ( initialHeaders [ INTERNAL_HEADER_RESOLVED_ROUTES ] )
71
72
: ( [ ] as ResolvedRoute [ ] ) ,
73
+ rewriteStatusCode : Number . parseInt (
74
+ initialHeaders [ INTERNAL_HEADER_REWRITE_STATUS_CODE ] ,
75
+ ) ,
72
76
} ;
73
77
74
78
let routingResult : InternalResult | RoutingResult = {
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ const middleMatch = getMiddlewareMatch(
28
28
type MiddlewareEvent = InternalEvent & {
29
29
responseHeaders ?: Record < string , string | string [ ] > ;
30
30
isExternalRewrite ?: boolean ;
31
+ rewriteStatusCode ?: number ;
31
32
} ;
32
33
33
34
type Middleware = ( request : Request ) => Response | Promise < Response > ;
@@ -189,5 +190,6 @@ export async function handleMiddleware(
189
190
cookies : internalEvent . cookies ,
190
191
remoteAddress : internalEvent . remoteAddress ,
191
192
isExternalRewrite,
193
+ rewriteStatusCode : statusCode ,
192
194
} satisfies MiddlewareEvent ;
193
195
}
Original file line number Diff line number Diff line change @@ -413,6 +413,7 @@ export function createServerResponse(
413
413
} ,
414
414
responseStream ,
415
415
headers ,
416
+ routingResult . rewriteStatusCode ,
416
417
) ;
417
418
}
418
419
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ export const INTERNAL_HEADER_PREFIX = "x-opennext-";
36
36
export const INTERNAL_HEADER_INITIAL_URL = `${ INTERNAL_HEADER_PREFIX } initial-url` ;
37
37
export const INTERNAL_HEADER_LOCALE = `${ INTERNAL_HEADER_PREFIX } locale` ;
38
38
export const INTERNAL_HEADER_RESOLVED_ROUTES = `${ INTERNAL_HEADER_PREFIX } resolved-routes` ;
39
+ export const INTERNAL_HEADER_REWRITE_STATUS_CODE = `${ INTERNAL_HEADER_PREFIX } rewrite-status-code` ;
39
40
export const INTERNAL_EVENT_REQUEST_ID = `${ INTERNAL_HEADER_PREFIX } request-id` ;
40
41
41
42
// Geolocation headers starting from Nextjs 15
@@ -254,6 +255,7 @@ export default async function routingHandler(
254
255
locale : NextConfig . i18n
255
256
? detectLocale ( eventOrResult , NextConfig . i18n )
256
257
: undefined ,
258
+ rewriteStatusCode : middlewareEventOrResult . rewriteStatusCode ,
257
259
} ;
258
260
} catch ( e ) {
259
261
error ( "Error in routingHandler" , e ) ;
Original file line number Diff line number Diff line change @@ -71,8 +71,12 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
71
71
private onEnd : ( headers : OutgoingHttpHeaders ) => Promise < void > ,
72
72
private streamCreator ?: StreamCreator ,
73
73
private initialHeaders ?: OutgoingHttpHeaders ,
74
+ statusCode ?: number ,
74
75
) {
75
76
super ( ) ;
77
+ if ( statusCode !== undefined ) {
78
+ this . statusCode = statusCode ;
79
+ }
76
80
}
77
81
78
82
// Necessary for next 12
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ export type InternalResult = {
37
37
headers : Record < string , string | string [ ] > ;
38
38
body : ReadableStream ;
39
39
isBase64Encoded : boolean ;
40
+ rewriteStatusCode ?: number ;
40
41
} & BaseEventOrResult < "core" > ;
41
42
42
43
export interface StreamCreator {
@@ -151,6 +152,8 @@ export interface RoutingResult {
151
152
152
153
// The resolved route after applying rewrites, if used with an external middleware will be defined in x-opennext-resolved-routes header as a json encoded array
153
154
resolvedRoutes : ResolvedRoute [ ] ;
155
+ // The status code applied to a middleware rewrite
156
+ rewriteStatusCode ?: number ;
154
157
}
155
158
156
159
export interface MiddlewareResult
You can’t perform that action at this time.
0 commit comments