Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-buttons-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: Propogate the status code in middleware rewrites
4 changes: 4 additions & 0 deletions packages/open-next/src/adapters/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { constructNextUrl } from "../core/routing/util";
import routingHandler, {
INTERNAL_EVENT_REQUEST_ID,
INTERNAL_HEADER_REWRITE_STATUS_CODE,
INTERNAL_HEADER_INITIAL_URL,
INTERNAL_HEADER_RESOLVED_ROUTES,
} from "../core/routingHandler";
Expand Down Expand Up @@ -86,6 +87,9 @@ const defaultHandler = async (
result.resolvedRoutes,
),
[INTERNAL_EVENT_REQUEST_ID]: requestId,
[INTERNAL_HEADER_REWRITE_STATUS_CODE]: String(
result.rewriteStatusCode,
),
},
},
isExternalRewrite: result.isExternalRewrite,
Expand Down
4 changes: 4 additions & 0 deletions packages/open-next/src/core/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "./routing/util";
import routingHandler, {
INTERNAL_EVENT_REQUEST_ID,
INTERNAL_HEADER_REWRITE_STATUS_CODE,
INTERNAL_HEADER_INITIAL_URL,
INTERNAL_HEADER_RESOLVED_ROUTES,
MIDDLEWARE_HEADER_PREFIX,
Expand Down Expand Up @@ -69,6 +70,9 @@ export async function openNextHandler(
resolvedRoutes: initialHeaders[INTERNAL_HEADER_RESOLVED_ROUTES]
? JSON.parse(initialHeaders[INTERNAL_HEADER_RESOLVED_ROUTES])
: ([] as ResolvedRoute[]),
rewriteStatusCode: Number.parseInt(
initialHeaders[INTERNAL_HEADER_REWRITE_STATUS_CODE],
),
};

let routingResult: InternalResult | RoutingResult = {
Expand Down
2 changes: 2 additions & 0 deletions packages/open-next/src/core/routing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const middleMatch = getMiddlewareMatch(
type MiddlewareEvent = InternalEvent & {
responseHeaders?: Record<string, string | string[]>;
isExternalRewrite?: boolean;
rewriteStatusCode?: number;
};

type Middleware = (request: Request) => Response | Promise<Response>;
Expand Down Expand Up @@ -189,5 +190,6 @@ export async function handleMiddleware(
cookies: internalEvent.cookies,
remoteAddress: internalEvent.remoteAddress,
isExternalRewrite,
rewriteStatusCode: statusCode,
} satisfies MiddlewareEvent;
}
1 change: 1 addition & 0 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export function createServerResponse(
},
responseStream,
headers,
routingResult.rewriteStatusCode,
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/open-next/src/core/routingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const INTERNAL_HEADER_PREFIX = "x-opennext-";
export const INTERNAL_HEADER_INITIAL_URL = `${INTERNAL_HEADER_PREFIX}initial-url`;
export const INTERNAL_HEADER_LOCALE = `${INTERNAL_HEADER_PREFIX}locale`;
export const INTERNAL_HEADER_RESOLVED_ROUTES = `${INTERNAL_HEADER_PREFIX}resolved-routes`;
export const INTERNAL_HEADER_REWRITE_STATUS_CODE = `${INTERNAL_HEADER_PREFIX}rewrite-status-code`;
export const INTERNAL_EVENT_REQUEST_ID = `${INTERNAL_HEADER_PREFIX}request-id`;

// Geolocation headers starting from Nextjs 15
Expand Down Expand Up @@ -254,6 +255,7 @@ export default async function routingHandler(
locale: NextConfig.i18n
? detectLocale(eventOrResult, NextConfig.i18n)
: undefined,
rewriteStatusCode: middlewareEventOrResult.rewriteStatusCode,
};
} catch (e) {
error("Error in routingHandler", e);
Expand Down
4 changes: 4 additions & 0 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
private onEnd: (headers: OutgoingHttpHeaders) => Promise<void>,
private streamCreator?: StreamCreator,
private initialHeaders?: OutgoingHttpHeaders,
statusCode?: number,
) {
super();
if (statusCode !== undefined) {
this.statusCode = statusCode;
}
}

// Necessary for next 12
Expand Down
2 changes: 2 additions & 0 deletions packages/open-next/src/types/open-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type InternalResult = {
headers: Record<string, string | string[]>;
body: ReadableStream;
isBase64Encoded: boolean;
rewriteStatusCode?: number;
} & BaseEventOrResult<"core">;

export interface StreamCreator {
Expand Down Expand Up @@ -151,6 +152,7 @@ export interface RoutingResult {

// 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
resolvedRoutes: ResolvedRoute[];
rewriteStatusCode?: number;
}

export interface MiddlewareResult
Expand Down
Loading