@@ -600,11 +600,13 @@ function normalizeHydrationNavigationUrl(url: string): string {
600600
601601class HrefInterpolationError extends Error { }
602602
603- function interpolateCurrentDynamicRoute ( resolved : string ) : string {
604- if ( typeof window === "undefined" ) return resolved ;
603+ type CurrentDynamicRoute = { href : string ; as : string } ;
604+
605+ function resolveCurrentDynamicRoute ( resolved : string ) : CurrentDynamicRoute | null {
606+ if ( typeof window === "undefined" ) return null ;
605607
606608 const routePattern = window . __NEXT_DATA__ ?. page ;
607- if ( ! routePattern || extractRouteParamNames ( routePattern ) . length === 0 ) return resolved ;
609+ if ( ! routePattern || extractRouteParamNames ( routePattern ) . length === 0 ) return null ;
608610
609611 try {
610612 const target = new URL ( resolved , "http://vinext.local" ) ;
@@ -614,14 +616,14 @@ function interpolateCurrentDynamicRoute(resolved: string): string {
614616 target . origin !== "http://vinext.local" &&
615617 target . origin !== currentOrigin
616618 ) {
617- return resolved ;
619+ return null ;
618620 }
619621 const visiblePath = stripBasePath ( window . location . pathname , __basePath ) ;
620622 const visibleLocale = getLocalePathPrefix ( visiblePath , window . __VINEXT_LOCALES__ ) ;
621623 const routePath = visibleLocale
622624 ? visiblePath . slice ( visibleLocale . length + 1 ) || "/"
623625 : visiblePath ;
624- if ( extractRouteParamsFromPath ( routePattern , routePath ) === null ) return resolved ;
626+ if ( extractRouteParamsFromPath ( routePattern , routePath ) === null ) return null ;
625627
626628 const query = parseQueryString ( target . search ) ;
627629 const missingParams = routePatternParts ( routePattern )
@@ -641,7 +643,7 @@ function interpolateCurrentDynamicRoute(resolved: string): string {
641643 }
642644
643645 const routeParams = getRouteParamsFromQuery ( routePattern , query ) ;
644- if ( ! routeParams ) return resolved ;
646+ if ( ! routeParams ) return null ;
645647
646648 const encodedRouteParams = Object . fromEntries (
647649 Object . entries ( routeParams ) . map ( ( [ key , value ] ) => [
@@ -650,19 +652,21 @@ function interpolateCurrentDynamicRoute(resolved: string): string {
650652 ] ) ,
651653 ) ;
652654 const pathname = fillRoutePatternSegments ( routePattern , encodedRouteParams ) ;
653- if ( ! pathname ) return resolved ;
655+ if ( ! pathname ) return null ;
654656
655657 const targetLocale = getLocalePathPrefix ( target . pathname , window . __VINEXT_LOCALES__ ) ;
658+ const hrefPathname = targetLocale ? `/${ targetLocale } ${ routePattern } ` : routePattern ;
659+ const href = hrefPathname + target . search + target . hash ;
656660 target . pathname = targetLocale ? `/${ targetLocale } ${ pathname } ` : pathname ;
657661 for ( const paramName of extractRouteParamNames ( routePattern ) ) {
658662 target . searchParams . delete ( paramName ) ;
659663 }
660- return target . href . slice ( target . origin . length ) ;
664+ return { href , as : target . href . slice ( target . origin . length ) } ;
661665 } catch ( error ) {
662666 if ( error instanceof HrefInterpolationError ) {
663667 throw error ;
664668 }
665- return resolved ;
669+ return null ;
666670 }
667671}
668672
@@ -3235,9 +3239,19 @@ async function performNavigation(
32353239 typeof url . query === "object" &&
32363240 Object . keys ( url . query ) . length > 0 ) ||
32373241 ( typeof url . search === "string" && url . search . length > 0 ) ) ) ) ;
3242+ let inheritedRouteHref : string | undefined ;
32383243 if ( inheritsCurrentPath ) {
3239- resolved = interpolateCurrentDynamicRoute ( resolved ) ;
3240- resolvedRoute = interpolateCurrentDynamicRoute ( resolvedRoute ) ;
3244+ const routeMatchesTarget = resolvedRoute === resolved ;
3245+ const resolvedDynamicRoute = resolveCurrentDynamicRoute ( resolved ) ;
3246+ if ( resolvedDynamicRoute ) resolved = resolvedDynamicRoute . as ;
3247+
3248+ const internalDynamicRoute = routeMatchesTarget
3249+ ? resolvedDynamicRoute
3250+ : resolveCurrentDynamicRoute ( resolvedRoute ) ;
3251+ if ( internalDynamicRoute ) {
3252+ resolvedRoute = internalDynamicRoute . as ;
3253+ inheritedRouteHref = internalDynamicRoute . href ;
3254+ }
32413255 }
32423256
32433257 // External URLs — delegate to browser (unless same-origin)
@@ -3435,7 +3449,11 @@ async function performNavigation(
34353449 const navStateOptions : { locale ?: string ; shallow : boolean } = { shallow } ;
34363450 if ( navigationLocale !== undefined ) navStateOptions . locale = navigationLocale ;
34373451 const resolvedNoHash = stripHash ( resolved ) ;
3438- const resolvedRouteNoHash = stripHash ( interpolatedRoute ) ;
3452+ const resolvedRouteNoHash = stripHash (
3453+ inheritedRouteHref
3454+ ? normalizePathTrailingSlash ( inheritedRouteHref , __trailingSlash )
3455+ : interpolatedRoute ,
3456+ ) ;
34393457 const navState = {
34403458 url : resolvedRouteNoHash ,
34413459 as : resolvedNoHash ,
@@ -3991,7 +4009,9 @@ function handlePagesRouterPopState(e: PopStateEvent): void {
39914009 typeof state . as === "string" &&
39924010 state . url !== state . as
39934011 ) {
3994- return normalizePathTrailingSlash ( withBasePath ( state . url , __basePath ) , __trailingSlash ) ;
4012+ const dynamicRoute = interpolateDynamicRouteHref ( state . url , state . as ) ;
4013+ const routeUrl = dynamicRoute ?. href || state . url ;
4014+ return normalizePathTrailingSlash ( withBasePath ( routeUrl , __basePath ) , __trailingSlash ) ;
39954015 }
39964016 return browserUrl ;
39974017 } ) ( ) ;
0 commit comments