Skip to content

Commit

Permalink
handle some middleware cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jun 28, 2022
1 parent 7fd4081 commit f80b9bc
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1216,22 +1216,30 @@ export default class Router implements BaseRouter {
handleHardNavigation({ url: as, router: this })
return true
}
resolvedAs = rewritesResult.asPath
if (!isMiddlewareMatch) {
resolvedAs = rewritesResult.asPath
}

if (rewritesResult.matchedPage && rewritesResult.resolvedHref) {
// if this directly matches a page we need to update the href to
// allow the correct page chunk to be loaded
pathname = rewritesResult.resolvedHref
parsed.pathname = addBasePath(pathname)
url = formatWithValidation(parsed)

if (!isMiddlewareMatch) {
url = formatWithValidation(parsed)
}
}
} else {
parsed.pathname = resolveDynamicRoute(pathname, pages)

if (parsed.pathname !== pathname) {
pathname = parsed.pathname
parsed.pathname = addBasePath(pathname)
url = formatWithValidation(parsed)

if (!isMiddlewareMatch) {
url = formatWithValidation(parsed)
}
}
}
}
Expand All @@ -1250,13 +1258,14 @@ export default class Router implements BaseRouter {
resolvedAs = removeLocale(removeBasePath(resolvedAs), nextState.locale)

let route = removeTrailingSlash(pathname)
let routeMatch: { [paramName: string]: string | string[] } | false = false

if (isDynamicRoute(route)) {
const parsedAs = parseRelativeUrl(resolvedAs)
const asPathname = parsedAs.pathname

const routeRegex = getRouteRegex(route)
const routeMatch = getRouteMatcher(routeRegex)(asPathname)
routeMatch = getRouteMatcher(routeRegex)(asPathname)
const shouldInterpolate = route === asPathname
const interpolatedAs = shouldInterpolate
? interpolateAs(route, asPathname, query)
Expand Down Expand Up @@ -1329,6 +1338,14 @@ export default class Router implements BaseRouter {
route = pathname
query = Object.assign({}, routeInfo.query || {}, query)

if (routeMatch && pathname !== parsed.pathname) {
Object.keys(routeMatch).forEach((key) => {
if (routeMatch && query[key] === routeMatch[key]) {
delete query[key]
}
})
}

if (isDynamicRoute(pathname)) {
const prefixedAs =
routeInfo.resolvedAs ||
Expand Down Expand Up @@ -1994,6 +2011,17 @@ export default class Router implements BaseRouter {
const pages = await this.pageLoader.getPageList()
let resolvedAs = asPath

const locale =
typeof options.locale !== 'undefined'
? options.locale || undefined
: this.locale

const isMiddlewareMatch = await matchesMiddleware({
asPath: asPath,
locale: locale,
router: this,
})

if (process.env.__NEXT_HAS_REWRITES && asPath.startsWith('/')) {
let rewrites: any
;({ __rewrites: rewrites } = await getClientBuildManifest())
Expand All @@ -2020,7 +2048,10 @@ export default class Router implements BaseRouter {
// allow the correct page chunk to be loaded
pathname = rewritesResult.resolvedHref
parsed.pathname = pathname
url = formatWithValidation(parsed)

if (!isMiddlewareMatch) {
url = formatWithValidation(parsed)
}
}
}
parsed.pathname = resolveDynamicRoute(parsed.pathname, pages)
Expand All @@ -2034,13 +2065,11 @@ export default class Router implements BaseRouter {
parsePath(asPath).pathname
) || {}
)
url = formatWithValidation(parsed)
}

const locale =
typeof options.locale !== 'undefined'
? options.locale || undefined
: this.locale
if (!isMiddlewareMatch) {
url = formatWithValidation(parsed)
}
}

// Prefetch is not supported in development mode because it would trigger on-demand-entries
if (process.env.NODE_ENV !== 'production') {
Expand Down

0 comments on commit f80b9bc

Please sign in to comment.