1
1
import type { NextApiResponse } from '../../types'
2
2
import type { IncomingMessage , ServerResponse } from 'node:http'
3
- import type { PrerenderManifest } from '..'
4
- import type { DevRoutesManifest } from '../../server/lib/router-utils/setup-dev-bundler'
5
- import type { InstrumentationOnRequestError } from '../../server/instrumentation/types'
6
3
7
4
import { sendError } from '../../server/api-utils'
8
5
import { RouteKind } from '../../server/route-kind'
@@ -15,26 +12,6 @@ import { hoist } from './helpers'
15
12
import * as userland from 'VAR_USERLAND'
16
13
import { getTracer , SpanKind } from '../../server/lib/trace/tracer'
17
14
import { BaseServerSpan } from '../../server/lib/trace/constants'
18
- import {
19
- ensureInstrumentationRegistered ,
20
- instrumentationOnRequestError ,
21
- } from '../../server/lib/router-utils/instrumentation-globals.external'
22
- import { getUtils } from '../../server/server-utils'
23
- import { PRERENDER_MANIFEST , ROUTES_MANIFEST } from '../../api/constants'
24
- import { isDynamicRoute } from '../../shared/lib/router/utils'
25
- import {
26
- RouterServerContextSymbol ,
27
- routerServerGlobal ,
28
- } from '../../server/lib/router-utils/router-server-context'
29
- import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'
30
- import {
31
- normalizeLocalePath ,
32
- type PathLocale ,
33
- } from '../../shared/lib/i18n/normalize-locale-path'
34
- import { loadManifestFromRelativePath } from '../../server/load-manifest.external'
35
- import { getHostname } from '../../shared/lib/get-hostname'
36
- import { detectDomainLocale } from '../../shared/lib/i18n/detect-domain-locale'
37
- import { parseReqUrl } from '../../lib/url'
38
15
39
16
// Re-export the handler (should be the default export).
40
17
export default hoist ( userland , 'default' )
@@ -53,6 +30,7 @@ const routeModule = new PagesAPIRouteModule({
53
30
filename : '' ,
54
31
} ,
55
32
userland,
33
+ distDir : process . env . __NEXT_RELATIVE_DIST_DIR || '' ,
56
34
} )
57
35
58
36
export async function handler (
@@ -62,23 +40,6 @@ export async function handler(
62
40
waitUntil ?: ( prom : Promise < void > ) => void
63
41
}
64
42
) : Promise < void > {
65
- const projectDir =
66
- routerServerGlobal [ RouterServerContextSymbol ] ?. dir || process . cwd ( )
67
- const distDir = process . env . __NEXT_RELATIVE_DIST_DIR || ''
68
- const isDev = process . env . NODE_ENV === 'development'
69
-
70
- const [ routesManifest , prerenderManifest ] = await Promise . all ( [
71
- loadManifestFromRelativePath < DevRoutesManifest > (
72
- projectDir ,
73
- distDir ,
74
- ROUTES_MANIFEST
75
- ) ,
76
- loadManifestFromRelativePath < PrerenderManifest > (
77
- projectDir ,
78
- distDir ,
79
- PRERENDER_MANIFEST
80
- ) ,
81
- ] )
82
43
let srcPage = 'VAR_DEFINITION_PAGE'
83
44
84
45
// turbopack doesn't normalize `/index` in the page name
@@ -87,95 +48,16 @@ export async function handler(
87
48
srcPage = srcPage . replace ( / \/ i n d e x $ / , '' )
88
49
}
89
50
90
- // We need to parse dynamic route params
91
- // and do URL normalization here.
92
- // TODO: move this into server-utils for re-use
93
- const { basePath, i18n, rewrites } = routesManifest
94
-
95
- if ( basePath ) {
96
- req . url = removePathPrefix ( req . url || '/' , basePath )
97
- }
98
-
99
- let localeResult : PathLocale | undefined
100
-
101
- if ( i18n ) {
102
- const urlParts = ( req . url || '/' ) . split ( '?' )
103
- localeResult = normalizeLocalePath ( urlParts [ 0 ] || '/' , i18n . locales )
104
-
105
- if ( localeResult . detectedLocale ) {
106
- req . url = `${ localeResult . pathname } ${
107
- urlParts [ 1 ] ? `?${ urlParts [ 1 ] } ` : ''
108
- } `
109
- }
110
- }
111
-
112
- const parsedUrl = parseReqUrl ( req . url || '/' )
51
+ const prepareResult = await routeModule . prepare ( req , srcPage )
113
52
114
- if ( ! parsedUrl ) {
53
+ if ( ! prepareResult ) {
115
54
res . statusCode = 400
116
55
res . end ( 'Bad Request' )
117
56
ctx . waitUntil ?.( Promise . resolve ( ) )
118
57
return
119
58
}
120
59
121
- const pageIsDynamic = isDynamicRoute ( srcPage )
122
-
123
- const serverUtils = getUtils ( {
124
- page : srcPage ,
125
- i18n,
126
- basePath,
127
- rewrites,
128
- pageIsDynamic,
129
- trailingSlash : process . env . __NEXT_TRAILING_SLASH as any as boolean ,
130
- caseSensitive : Boolean ( routesManifest . caseSensitive ) ,
131
- } )
132
-
133
- const domainLocale = detectDomainLocale (
134
- i18n ?. domains ,
135
- getHostname ( parsedUrl , req . headers ) ,
136
- localeResult ?. detectedLocale
137
- )
138
-
139
- const defaultLocale = domainLocale ?. defaultLocale || i18n ?. defaultLocale
140
-
141
- // Ensure parsedUrl.pathname includes locale before processing
142
- // rewrites or they won't match correctly.
143
- if ( defaultLocale && ! localeResult ?. detectedLocale ) {
144
- parsedUrl . pathname = `/${ defaultLocale } ${ parsedUrl . pathname } `
145
- }
146
-
147
- const rewriteParamKeys = Object . keys (
148
- serverUtils . handleRewrites ( req , parsedUrl )
149
- )
150
- serverUtils . normalizeCdnUrl ( req , [
151
- ...rewriteParamKeys ,
152
- ...Object . keys ( serverUtils . defaultRouteRegex ?. groups || { } ) ,
153
- ] )
154
-
155
- const params : Record < string , undefined | string | string [ ] > =
156
- serverUtils . dynamicRouteMatcher
157
- ? serverUtils . dynamicRouteMatcher (
158
- localeResult ?. pathname || parsedUrl . pathname || ''
159
- ) || { }
160
- : { }
161
-
162
- const query = {
163
- ...parsedUrl . query ,
164
- ...params ,
165
- }
166
- serverUtils . normalizeQueryParams ( query )
167
-
168
- if ( pageIsDynamic ) {
169
- const result = serverUtils . normalizeDynamicRouteParams ( query , true )
170
-
171
- if ( result . hasValidParams ) {
172
- Object . assign ( query , result . params )
173
- }
174
- }
175
-
176
- // ensure instrumentation is registered and pass
177
- // onRequestError below
178
- await ensureInstrumentationRegistered ( projectDir , distDir )
60
+ const { query, params, prerenderManifest } = prepareResult
179
61
180
62
try {
181
63
const method = req . method || 'GET'
@@ -197,11 +79,10 @@ export async function handler(
197
79
// doesn't need to load
198
80
previewProps : prerenderManifest . preview ,
199
81
propagateError : false ,
200
- dev : isDev ,
82
+ dev : routeModule . isDev ,
201
83
page : 'VAR_DEFINITION_PAGE' ,
202
84
203
- onError : ( ...args : Parameters < InstrumentationOnRequestError > ) =>
204
- instrumentationOnRequestError ( projectDir , distDir , ...args ) ,
85
+ onError : routeModule . instrumentationOnRequestError . bind ( routeModule ) ,
205
86
} )
206
87
. finally ( ( ) => {
207
88
if ( ! span ) return
@@ -266,7 +147,7 @@ export async function handler(
266
147
}
267
148
} catch ( err ) {
268
149
// we re-throw in dev to show the error overlay
269
- if ( isDev ) {
150
+ if ( routeModule . isDev ) {
270
151
throw err
271
152
}
272
153
// this is technically an invariant as error handling
0 commit comments