1
- import { parse as parseQs } from 'querystring'
2
1
import { IncomingMessage , ServerResponse } from 'http'
3
2
import { parse as parseUrl , format as formatUrl , UrlWithParsedQuery } from 'url'
4
3
import { isResSent } from '../../../../next-server/lib/utils'
@@ -14,7 +13,6 @@ import {
14
13
} from '../../../../next-server/server/api-utils'
15
14
import { getRedirectStatus } from '../../../../lib/load-custom-routes'
16
15
import getRouteNoAssetPath from '../../../../next-server/lib/router/utils/get-route-from-asset-path'
17
- import { getRouteMatcher } from '../../../../next-server/lib/router/utils/route-matcher'
18
16
import { PERMANENT_REDIRECT_STATUS } from '../../../../next-server/lib/constants'
19
17
20
18
export function getPageHandler ( ctx : ServerlessHandlerCtx ) {
@@ -56,7 +54,10 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
56
54
handleBasePath,
57
55
defaultRouteRegex,
58
56
dynamicRouteMatcher,
57
+ interpolateDynamicPath,
58
+ getParamsFromRouteMatches,
59
59
normalizeDynamicRouteParams,
60
+ normalizeVercelUrl,
60
61
} = getUtils ( ctx )
61
62
62
63
async function renderReqToHTML (
@@ -222,116 +223,24 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
222
223
! hasValidParams &&
223
224
req . headers ?. [ 'x-now-route-matches' ]
224
225
) {
225
- nowParams = getRouteMatcher (
226
- ( function ( ) {
227
- const { groups, routeKeys } = defaultRouteRegex !
228
-
229
- return {
230
- re : {
231
- // Simulate a RegExp match from the \`req.url\` input
232
- exec : ( str : string ) => {
233
- const obj = parseQs ( str )
234
-
235
- // favor named matches if available
236
- const routeKeyNames = Object . keys ( routeKeys || { } )
237
-
238
- const filterLocaleItem = ( val : string | string [ ] ) => {
239
- if ( i18n ) {
240
- // locale items can be included in route-matches
241
- // for fallback SSG pages so ensure they are
242
- // filtered
243
- const isCatchAll = Array . isArray ( val )
244
- const _val = isCatchAll ? val [ 0 ] : val
245
-
246
- if (
247
- typeof _val === 'string' &&
248
- i18n . locales . some ( ( item ) => {
249
- if ( item . toLowerCase ( ) === _val . toLowerCase ( ) ) {
250
- detectedLocale = item
251
- renderOpts . locale = detectedLocale
252
- return true
253
- }
254
- return false
255
- } )
256
- ) {
257
- // remove the locale item from the match
258
- if ( isCatchAll ) {
259
- ; ( val as string [ ] ) . splice ( 0 , 1 )
260
- }
261
-
262
- // the value is only a locale item and
263
- // shouldn't be added
264
- return isCatchAll ? val . length === 0 : true
265
- }
266
- }
267
- return false
268
- }
269
-
270
- if ( routeKeyNames . every ( ( name ) => obj [ name ] ) ) {
271
- return routeKeyNames . reduce ( ( prev , keyName ) => {
272
- const paramName = routeKeys ?. [ keyName ]
273
-
274
- if ( paramName && ! filterLocaleItem ( obj [ keyName ] ) ) {
275
- prev [ groups [ paramName ] . pos ] = obj [ keyName ]
276
- }
277
- return prev
278
- } , { } as any )
279
- }
280
-
281
- return Object . keys ( obj ) . reduce ( ( prev , key ) => {
282
- if ( ! filterLocaleItem ( obj [ key ] ) ) {
283
- return Object . assign ( prev , {
284
- [ key ] : obj [ key ] ,
285
- } )
286
- }
287
- return prev
288
- } , { } )
289
- } ,
290
- } ,
291
- groups,
292
- }
293
- } ) ( ) as any
294
- ) ( req . headers [ 'x-now-route-matches' ] as string )
226
+ nowParams = getParamsFromRouteMatches ( req , renderOpts , detectedLocale )
295
227
}
296
228
297
229
// make sure to set renderOpts to the correct params e.g. _params
298
230
// if provided from worker or params if we're parsing them here
299
231
renderOpts . params = _params || params
300
232
301
- // make sure to normalize req.url on Vercel to strip dynamic params
302
- // from the query which are added during routing
303
- if ( pageIsDynamic && trustQuery && defaultRouteRegex ) {
304
- const _parsedUrl = parseUrl ( req . url ! , true )
305
- delete ( _parsedUrl as any ) . search
306
-
307
- for ( const param of Object . keys ( defaultRouteRegex . groups ) ) {
308
- delete _parsedUrl . query [ param ]
309
- }
310
- req . url = formatUrl ( _parsedUrl )
311
- }
233
+ normalizeVercelUrl ( req , ! ! trustQuery )
312
234
313
235
// normalize request URL/asPath for fallback/revalidate pages since the
314
236
// proxy sets the request URL to the output's path for fallback pages
315
237
if ( pageIsDynamic && nowParams && defaultRouteRegex ) {
316
238
const _parsedUrl = parseUrl ( req . url ! )
317
239
318
- for ( const param of Object . keys ( defaultRouteRegex . groups ) ) {
319
- const { optional, repeat } = defaultRouteRegex . groups [ param ]
320
- let builtParam = `[${ repeat ? '...' : '' } ${ param } ]`
321
-
322
- if ( optional ) {
323
- builtParam = `[${ builtParam } ]`
324
- }
325
-
326
- const paramIdx = _parsedUrl . pathname ! . indexOf ( builtParam )
327
-
328
- if ( paramIdx > - 1 ) {
329
- _parsedUrl . pathname =
330
- _parsedUrl . pathname ! . substr ( 0 , paramIdx ) +
331
- encodeURI ( ( nowParams as any ) [ param ] || '' ) +
332
- _parsedUrl . pathname ! . substr ( paramIdx + builtParam . length )
333
- }
334
- }
240
+ _parsedUrl . pathname = interpolateDynamicPath (
241
+ _parsedUrl . pathname ! ,
242
+ nowParams
243
+ )
335
244
parsedUrl . pathname = _parsedUrl . pathname
336
245
req . url = formatUrl ( _parsedUrl )
337
246
}
0 commit comments