@@ -26,6 +26,9 @@ import { handleMiddleware } from "./routing/middleware";
2626
2727export const MIDDLEWARE_HEADER_PREFIX = "x-middleware-response-" ;
2828export const INTERNAL_HEADER_PREFIX = "x-opennext-" ;
29+ export const INTERNAL_HEADER_INITIAL_PATH = `${ INTERNAL_HEADER_PREFIX } initial-path` ;
30+ export const INTERNAL_HEADER_RESOLVED_ROUTE = `${ INTERNAL_HEADER_PREFIX } resolved-route` ;
31+ export const INTERNAL_HEADER_ROUTE_TYPE = `${ INTERNAL_HEADER_PREFIX } route-type` ;
2932export const MIDDLEWARE_HEADER_PREFIX_LEN = MIDDLEWARE_HEADER_PREFIX . length ;
3033
3134// Add the locale prefix to the regex so we correctly match the rawPath
@@ -191,7 +194,7 @@ export default async function routingHandler(
191194 const foundDynamicRoute = dynamicRouteMatcher ( internalEvent . rawPath ) ;
192195 const isDynamicRoute = ! isExternalRewrite && Boolean ( foundDynamicRoute ) ;
193196
194- if ( ! isDynamicRoute && ! isStaticRoute && ! isExternalRewrite ) {
197+ if ( ! ( isDynamicRoute || isStaticRoute || isExternalRewrite ) ) {
195198 // Fallback rewrite to be applied
196199 const fallbackRewrites = handleRewrites (
197200 internalEvent ,
@@ -216,12 +219,14 @@ export default async function routingHandler(
216219 // If we still haven't found a route, we show the 404 page
217220 // We need to ensure that rewrites are applied before showing the 404 page
218221 if (
219- ! isRouteFoundBeforeAllRewrites &&
220- ! isApiRoute &&
221- ! isNextImageRoute &&
222- // We need to check again once all rewrites have been applied
223- ! staticRouteMatcher ( internalEvent . rawPath ) &&
224- ! dynamicRouteMatcher ( internalEvent . rawPath )
222+ ! (
223+ isRouteFoundBeforeAllRewrites ||
224+ isApiRoute ||
225+ isNextImageRoute ||
226+ // We need to check again once all rewrites have been applied
227+ staticRouteMatcher ( internalEvent . rawPath ) ||
228+ dynamicRouteMatcher ( internalEvent . rawPath )
229+ )
225230 ) {
226231 internalEvent = {
227232 ...internalEvent ,
@@ -260,20 +265,20 @@ export default async function routingHandler(
260265 ...nextHeaders ,
261266 } ) ;
262267
263- const resolvedRoute = foundStaticRoute
264- ? foundStaticRoute . page
265- : foundDynamicRoute
266- ? foundDynamicRoute . page
267- : undefined ;
268+ let resolvedRoute : string | undefined ;
269+ let routeType : RouteType | undefined ;
270+
271+ if ( foundStaticRoute ) {
272+ resolvedRoute = foundStaticRoute . page ;
273+ routeType = foundStaticRoute . routeType ;
274+ } else if ( foundDynamicRoute ) {
275+ resolvedRoute = foundDynamicRoute . page ;
276+ routeType = foundDynamicRoute . routeType ;
277+ } else if ( isApiRoute ) {
278+ // For /api paths we assume that they're route types
279+ routeType = "route" ;
280+ }
268281
269- const routeType = foundStaticRoute
270- ? foundStaticRoute . routeType
271- : foundDynamicRoute
272- ? foundDynamicRoute . routeType
273- : // For /api paths we assume that they're route types
274- internalEvent . rawPath . startsWith ( "/api" )
275- ? "route"
276- : undefined ;
277282 return {
278283 internalEvent,
279284 isExternalRewrite,
0 commit comments