7
7
import type {
8
8
InternalEvent ,
9
9
InternalResult ,
10
+ ResolvedRoute ,
10
11
RoutingResult ,
11
12
} from "types/open-next" ;
12
13
@@ -20,42 +21,17 @@ import {
20
21
handleRewrites ,
21
22
} from "./routing/matcher" ;
22
23
import { handleMiddleware } from "./routing/middleware" ;
24
+ import {
25
+ apiPrefix ,
26
+ dynamicRouteMatcher ,
27
+ staticRouteMatcher ,
28
+ } from "./routing/routeMatcher" ;
23
29
24
30
export const MIDDLEWARE_HEADER_PREFIX = "x-middleware-response-" ;
25
31
export 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` ;
59
35
60
36
// Geolocation headers starting from Nextjs 15
61
37
// See https://github.com/vercel/vercel/blob/7714b1c/packages/functions/src/headers.ts
@@ -95,6 +71,17 @@ export default async function routingHandler(
95
71
}
96
72
}
97
73
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
+
98
85
const nextHeaders = getNextConfigHeaders ( event , ConfigHeaders ) ;
99
86
100
87
let internalEvent = fixDataPage ( event , BuildId ) ;
@@ -127,14 +114,10 @@ export default async function routingHandler(
127
114
internalEvent = beforeRewrites . internalEvent ;
128
115
isExternalRewrite = beforeRewrites . isExternalRewrite ;
129
116
}
117
+ const foundStaticRoute = staticRouteMatcher ( internalEvent . rawPath ) ;
118
+ const isStaticRoute = ! isExternalRewrite && foundStaticRoute . length > 0 ;
130
119
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 ) ) {
138
121
// Second rewrite to be applied
139
122
const afterRewrites = handleRewrites (
140
123
internalEvent ,
@@ -151,12 +134,10 @@ export default async function routingHandler(
151
134
) ;
152
135
internalEvent = fallbackEvent ;
153
136
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 ) ) {
160
141
// Fallback rewrite to be applied
161
142
const fallbackRewrites = handleRewrites (
162
143
internalEvent ,
@@ -181,15 +162,13 @@ export default async function routingHandler(
181
162
// If we still haven't found a route, we show the 404 page
182
163
// We need to ensure that rewrites are applied before showing the 404 page
183
164
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
193
172
)
194
173
) {
195
174
internalEvent = {
@@ -229,10 +208,19 @@ export default async function routingHandler(
229
208
...nextHeaders ,
230
209
} ) ;
231
210
211
+ const resolvedRoutes : ResolvedRoute [ ] = [
212
+ ...foundStaticRoute ,
213
+ ...foundDynamicRoute ,
214
+ ] ;
215
+
216
+ debug ( "resolvedRoutes" , resolvedRoutes ) ;
217
+
232
218
return {
233
219
internalEvent,
234
220
isExternalRewrite,
235
221
origin : false ,
236
222
isISR,
223
+ initialPath : event . rawPath ,
224
+ resolvedRoutes,
237
225
} ;
238
226
}
0 commit comments