Skip to content

Commit

Permalink
Iteration 4
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Jun 16, 2022
1 parent 070eec5 commit 06ca551
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions packages/next/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { RouteHas } from '../lib/load-custom-routes'
import { matchHas } from '../shared/lib/router/utils/prepare-destination'
import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix'
import { getRequestMeta } from './request-meta'
import { addPathPrefix } from '../shared/lib/router/utils/add-path-prefix'
import { formatNextPathnameInfo } from '../shared/lib/router/utils/format-next-pathname-info'
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'

type RouteResult = {
Expand Down Expand Up @@ -299,8 +299,7 @@ export default class Router {
// in the pathname here to allow custom-routes to require containing
// it or not, filesystem routes and pages must always include the basePath
// if it is set
let currentPathname = parsedUrlUpdated.pathname as string
const originalPathname = currentPathname
const originalPathname = parsedUrlUpdated.pathname as string
const pathnameInfo = getNextPathnameInfo(originalPathname, {
nextConfig: this.nextConfig,
parseData: false,
Expand All @@ -317,17 +316,12 @@ export default class Router {
continue
}

const currentPathnameNoBasePath = removePathPrefix(
currentPathname,
this.basePath
)

if (
!customRouteTypes.has(testRoute.type) &&
testRoute.name !== 'public folder catchall' &&
testRoute.name !== 'middleware catchall'
) {
currentPathname = currentPathnameNoBasePath
pathnameInfo.basePath = ''
}

if (
Expand All @@ -336,46 +330,39 @@ export default class Router {
!pathnameInfo.locale &&
parsedUrl.query.__nextLocale
) {
currentPathname = addPathPrefix(
`/${parsedUrl.query.__nextLocale}${
currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath
}`,
pathnameInfo.locale = parsedUrl.query.__nextLocale
pathnameInfo.basePath =
customRouteTypes.has(testRoute.type) ||
testRoute.name === 'public folder catchall' ||
testRoute.name === 'middleware catchall'
testRoute.name === 'public folder catchall' ||
testRoute.name === 'middleware catchall'
? this.basePath
: undefined
)
}

// Make sure the trailing slash is there before matching
if (
customRouteTypes.has(testRoute.type) &&
getRequestMeta(req, '__nextHadTrailingSlash') &&
!currentPathname.endsWith('/')
getRequestMeta(req, '__nextHadTrailingSlash')
) {
currentPathname += '/'
pathnameInfo.trailingSlash = true
}

if (!customRouteTypes.has(testRoute.type)) {
const prepend =
pathnameInfo.basePath =
getRequestMeta(req, '_nextHadBasePath') &&
(customRouteTypes.has(testRoute.type) ||
testRoute.name === 'public folder catchall' ||
testRoute.name === 'middleware catchall')
? this.basePath
: ''
currentPathname = `${prepend}${
(customRouteTypes.has(testRoute.type) ||
testRoute.name === 'public folder catchall' ||
testRoute.name === 'middleware catchall') &&
currentPathnameNoBasePath === '/'
? ''
: currentPathnameNoBasePath
}`
}

let newParams = testRoute.match(currentPathname)
let newParams = testRoute.match(
formatNextPathnameInfo({
ignorePrefix: true,
...pathnameInfo,
})
)

if (testRoute.has && newParams) {
const hasParams = matchHas(req, testRoute.has, parsedUrlUpdated.query)
Expand Down Expand Up @@ -409,7 +396,10 @@ export default class Router {
continue
}

parsedUrlUpdated.pathname = currentPathname
parsedUrlUpdated.pathname = formatNextPathnameInfo({
ignorePrefix: true,
...pathnameInfo,
})
}

const result = await testRoute.fn(
Expand Down

0 comments on commit 06ca551

Please sign in to comment.