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 {
2020import { constructNextUrl } from "../core/routing/util" ;
2121import routingHandler , {
2222 INTERNAL_EVENT_REQUEST_ID ,
23+ INTERNAL_HEADER_REWRITE_STATUS_CODE ,
2324 INTERNAL_HEADER_INITIAL_URL ,
2425 INTERNAL_HEADER_RESOLVED_ROUTES ,
2526} from "../core/routingHandler" ;
@@ -86,6 +87,9 @@ const defaultHandler = async (
8687 result . resolvedRoutes ,
8788 ) ,
8889 [ INTERNAL_EVENT_REQUEST_ID ] : requestId ,
90+ [ INTERNAL_HEADER_REWRITE_STATUS_CODE ] : String (
91+ result . rewriteStatusCode ,
92+ ) ,
8993 } ,
9094 } ,
9195 isExternalRewrite : result . isExternalRewrite ,
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import {
2323} from "./routing/util" ;
2424import routingHandler , {
2525 INTERNAL_EVENT_REQUEST_ID ,
26+ INTERNAL_HEADER_REWRITE_STATUS_CODE ,
2627 INTERNAL_HEADER_INITIAL_URL ,
2728 INTERNAL_HEADER_RESOLVED_ROUTES ,
2829 MIDDLEWARE_HEADER_PREFIX ,
@@ -69,6 +70,9 @@ export async function openNextHandler(
6970 resolvedRoutes : initialHeaders [ INTERNAL_HEADER_RESOLVED_ROUTES ]
7071 ? JSON . parse ( initialHeaders [ INTERNAL_HEADER_RESOLVED_ROUTES ] )
7172 : ( [ ] as ResolvedRoute [ ] ) ,
73+ rewriteStatusCode : Number . parseInt (
74+ initialHeaders [ INTERNAL_HEADER_REWRITE_STATUS_CODE ] ,
75+ ) ,
7276 } ;
7377
7478 let routingResult : InternalResult | RoutingResult = {
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ const middleMatch = getMiddlewareMatch(
2828type MiddlewareEvent = InternalEvent & {
2929 responseHeaders ?: Record < string , string | string [ ] > ;
3030 isExternalRewrite ?: boolean ;
31+ rewriteStatusCode ?: number ;
3132} ;
3233
3334type Middleware = ( request : Request ) => Response | Promise < Response > ;
@@ -189,5 +190,6 @@ export async function handleMiddleware(
189190 cookies : internalEvent . cookies ,
190191 remoteAddress : internalEvent . remoteAddress ,
191192 isExternalRewrite,
193+ rewriteStatusCode : statusCode ,
192194 } satisfies MiddlewareEvent ;
193195}
Original file line number Diff line number Diff line change @@ -413,6 +413,7 @@ export function createServerResponse(
413413 } ,
414414 responseStream ,
415415 headers ,
416+ routingResult . rewriteStatusCode ,
416417 ) ;
417418}
418419
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ export const INTERNAL_HEADER_PREFIX = "x-opennext-";
3636export const INTERNAL_HEADER_INITIAL_URL = `${ INTERNAL_HEADER_PREFIX } initial-url` ;
3737export const INTERNAL_HEADER_LOCALE = `${ INTERNAL_HEADER_PREFIX } locale` ;
3838export const INTERNAL_HEADER_RESOLVED_ROUTES = `${ INTERNAL_HEADER_PREFIX } resolved-routes` ;
39+ export const INTERNAL_HEADER_REWRITE_STATUS_CODE = `${ INTERNAL_HEADER_PREFIX } rewrite-status-code` ;
3940export const INTERNAL_EVENT_REQUEST_ID = `${ INTERNAL_HEADER_PREFIX } request-id` ;
4041
4142// Geolocation headers starting from Nextjs 15
@@ -254,6 +255,7 @@ export default async function routingHandler(
254255 locale : NextConfig . i18n
255256 ? detectLocale ( eventOrResult , NextConfig . i18n )
256257 : undefined ,
258+ rewriteStatusCode : middlewareEventOrResult . rewriteStatusCode ,
257259 } ;
258260 } catch ( e ) {
259261 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 {
7171 private onEnd : ( headers : OutgoingHttpHeaders ) => Promise < void > ,
7272 private streamCreator ?: StreamCreator ,
7373 private initialHeaders ?: OutgoingHttpHeaders ,
74+ statusCode ?: number ,
7475 ) {
7576 super ( ) ;
77+ if ( statusCode !== undefined ) {
78+ this . statusCode = statusCode ;
79+ }
7680 }
7781
7882 // Necessary for next 12
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ export type InternalResult = {
3737 headers : Record < string , string | string [ ] > ;
3838 body : ReadableStream ;
3939 isBase64Encoded : boolean ;
40+ rewriteStatusCode ?: number ;
4041} & BaseEventOrResult < "core" > ;
4142
4243export interface StreamCreator {
@@ -151,6 +152,8 @@ export interface RoutingResult {
151152
152153 // 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
153154 resolvedRoutes : ResolvedRoute [ ] ;
155+ // The status code applied to a middleware rewrite
156+ rewriteStatusCode ?: number ;
154157}
155158
156159export interface MiddlewareResult
You can’t perform that action at this time.
0 commit comments