77import type {
88 InternalEvent ,
99 InternalResult ,
10+ ResolvedRoute ,
1011 RoutingResult ,
1112} from "types/open-next" ;
1213
@@ -20,42 +21,17 @@ import {
2021 handleRewrites ,
2122} from "./routing/matcher" ;
2223import { handleMiddleware } from "./routing/middleware" ;
24+ import {
25+ apiPrefix ,
26+ dynamicRouteMatcher ,
27+ staticRouteMatcher ,
28+ } from "./routing/routeMatcher" ;
2329
2430export const MIDDLEWARE_HEADER_PREFIX = "x-middleware-response-" ;
2531export const MIDDLEWARE_HEADER_PREFIX_LEN = MIDDLEWARE_HEADER_PREFIX . length ;
26-
27- // Add the locale prefix to the regex so we correctly match the rawPath
28- const optionalLocalePrefixRegex = RoutesManifest . locales . length
29- ? `^/(?:${ RoutesManifest . locales . map ( ( locale ) => `${ locale } /?` ) . join ( "|" ) } )?`
30- : "^/" ;
31-
32- // Add the basepath prefix to the regex so we correctly match the rawPath
33- const optionalBasepathPrefixRegex = RoutesManifest . basePath
34- ? `^${ RoutesManifest . basePath } /?`
35- : "^/" ;
36-
37- // Add the basePath prefix to the api routes
38- const apiPrefix = RoutesManifest . basePath
39- ? `${ RoutesManifest . basePath } /api`
40- : "/api" ;
41-
42- const staticRegexp = RoutesManifest . routes . static . map (
43- ( route ) =>
44- new RegExp (
45- route . regex
46- . replace ( "^/" , optionalLocalePrefixRegex )
47- . replace ( "^/" , optionalBasepathPrefixRegex ) ,
48- ) ,
49- ) ;
50-
51- const dynamicRegexp = RoutesManifest . routes . dynamic . map (
52- ( route ) =>
53- new RegExp (
54- route . regex
55- . replace ( "^/" , optionalLocalePrefixRegex )
56- . replace ( "^/" , optionalBasepathPrefixRegex ) ,
57- ) ,
58- ) ;
32+ export const INTERNAL_HEADER_PREFIX = "x-opennext-" ;
33+ export const INTERNAL_HEADER_INITIAL_PATH = `${ INTERNAL_HEADER_PREFIX } initial-path` ;
34+ export const INTERNAL_HEADER_RESOLVED_ROUTES = `${ INTERNAL_HEADER_PREFIX } resolved-routes` ;
5935
6036// Geolocation headers starting from Nextjs 15
6137// See https://github.com/vercel/vercel/blob/7714b1c/packages/functions/src/headers.ts
@@ -95,6 +71,17 @@ export default async function routingHandler(
9571 }
9672 }
9773
74+ // First we remove internal headers
75+ // We don't want to allow users to set these headers
76+ for ( const key of Object . keys ( event . headers ) ) {
77+ if (
78+ key . startsWith ( INTERNAL_HEADER_PREFIX ) ||
79+ key . startsWith ( MIDDLEWARE_HEADER_PREFIX )
80+ ) {
81+ delete event . headers [ key ] ;
82+ }
83+ }
84+
9885 const nextHeaders = getNextConfigHeaders ( event , ConfigHeaders ) ;
9986
10087 let internalEvent = fixDataPage ( event , BuildId ) ;
@@ -127,14 +114,10 @@ export default async function routingHandler(
127114 internalEvent = beforeRewrites . internalEvent ;
128115 isExternalRewrite = beforeRewrites . isExternalRewrite ;
129116 }
117+ const foundStaticRoute = staticRouteMatcher ( internalEvent . rawPath ) ;
118+ const isStaticRoute = ! isExternalRewrite && foundStaticRoute . length > 0 ;
130119
131- const isStaticRoute =
132- ! isExternalRewrite &&
133- staticRegexp . some ( ( route ) =>
134- route . test ( ( internalEvent as InternalEvent ) . rawPath ) ,
135- ) ;
136-
137- if ( ! isStaticRoute && ! isExternalRewrite ) {
120+ if ( ! ( isStaticRoute || isExternalRewrite ) ) {
138121 // Second rewrite to be applied
139122 const afterRewrites = handleRewrites (
140123 internalEvent ,
@@ -151,12 +134,10 @@ export default async function routingHandler(
151134 ) ;
152135 internalEvent = fallbackEvent ;
153136
154- const isDynamicRoute =
155- ! isExternalRewrite &&
156- dynamicRegexp . some ( ( route ) =>
157- route . test ( ( internalEvent as InternalEvent ) . rawPath ) ,
158- ) ;
159- if ( ! isDynamicRoute && ! isStaticRoute && ! isExternalRewrite ) {
137+ const foundDynamicRoute = dynamicRouteMatcher ( internalEvent . rawPath ) ;
138+ const isDynamicRoute = ! isExternalRewrite && foundDynamicRoute . length > 0 ;
139+
140+ if ( ! ( isDynamicRoute || isStaticRoute || isExternalRewrite ) ) {
160141 // Fallback rewrite to be applied
161142 const fallbackRewrites = handleRewrites (
162143 internalEvent ,
@@ -181,15 +162,13 @@ export default async function routingHandler(
181162 // If we still haven't found a route, we show the 404 page
182163 // We need to ensure that rewrites are applied before showing the 404 page
183164 if (
184- ! isRouteFoundBeforeAllRewrites &&
185- ! isApiRoute &&
186- ! isNextImageRoute &&
187- // We need to check again once all rewrites have been applied
188- ! staticRegexp . some ( ( route ) =>
189- route . test ( ( internalEvent as InternalEvent ) . rawPath ) ,
190- ) &&
191- ! dynamicRegexp . some ( ( route ) =>
192- route . test ( ( internalEvent as InternalEvent ) . rawPath ) ,
165+ ! (
166+ isRouteFoundBeforeAllRewrites ||
167+ isApiRoute ||
168+ isNextImageRoute ||
169+ // We need to check again once all rewrites have been applied
170+ staticRouteMatcher ( internalEvent . rawPath ) . length > 0 ||
171+ dynamicRouteMatcher ( internalEvent . rawPath ) . length > 0
193172 )
194173 ) {
195174 internalEvent = {
@@ -229,10 +208,19 @@ export default async function routingHandler(
229208 ...nextHeaders ,
230209 } ) ;
231210
211+ const resolvedRoutes : ResolvedRoute [ ] = [
212+ ...foundStaticRoute ,
213+ ...foundDynamicRoute ,
214+ ] ;
215+
216+ debug ( "resolvedRoutes" , resolvedRoutes ) ;
217+
232218 return {
233219 internalEvent,
234220 isExternalRewrite,
235221 origin : false ,
236222 isISR,
223+ initialPath : event . rawPath ,
224+ resolvedRoutes,
237225 } ;
238226}
0 commit comments