66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { APP_BASE_HREF , PlatformLocation } from '@angular/common' ;
9+ import { LocationStrategy } from '@angular/common' ;
1010import {
1111 ApplicationRef ,
1212 type PlatformRef ,
@@ -21,7 +21,7 @@ import {
2121 platformServer ,
2222 ɵrenderInternal as renderInternal ,
2323} from '@angular/platform-server' ;
24- import { ActivatedRoute , Router } from '@angular/router' ;
24+ import { ActivatedRoute , Router , UrlSerializer } from '@angular/router' ;
2525import { Console } from '../console' ;
2626import { joinUrlParts , stripIndexHtmlFromURL } from './url' ;
2727
@@ -60,12 +60,12 @@ export async function renderAngular(
6060 serverContext : string ,
6161) : Promise < { hasNavigationError : boolean ; redirectTo ?: string ; content : ( ) => Promise < string > } > {
6262 // A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
63- const urlToRender = stripIndexHtmlFromURL ( url ) . toString ( ) ;
63+ const urlToRender = stripIndexHtmlFromURL ( url ) ;
6464 const platformRef = platformServer ( [
6565 {
6666 provide : INITIAL_CONFIG ,
6767 useValue : {
68- url : urlToRender ,
68+ url : urlToRender . href ,
6969 document : html ,
7070 } ,
7171 } ,
@@ -96,31 +96,27 @@ export async function renderAngular(
9696 applicationRef = await bootstrap ( { platformRef } ) ;
9797 }
9898
99- const envInjector = applicationRef . injector ;
100- const router = envInjector . get ( Router ) ;
101- const initialUrl = router . currentNavigation ( ) ?. initialUrl . toString ( ) ;
102-
10399 // Block until application is stable.
104100 await applicationRef . whenStable ( ) ;
105101
106102 // TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
103+ const envInjector = applicationRef . injector ;
107104 const routerIsProvided = ! ! envInjector . get ( ActivatedRoute , null ) ;
105+ const router = envInjector . get ( Router ) ;
108106 const lastSuccessfulNavigation = router . lastSuccessfulNavigation ( ) ;
109107
110108 if ( ! routerIsProvided ) {
111109 hasNavigationError = false ;
112- } else if ( lastSuccessfulNavigation ?. finalUrl && initialUrl !== null ) {
110+ } else if ( lastSuccessfulNavigation ?. finalUrl ) {
113111 hasNavigationError = false ;
114112
115- const { finalUrl } = lastSuccessfulNavigation ;
116- const finalUrlStringified = finalUrl . toString ( ) ;
117-
118- if ( initialUrl !== finalUrlStringified ) {
119- const baseHref =
120- envInjector . get ( APP_BASE_HREF , null , { optional : true } ) ??
121- envInjector . get ( PlatformLocation ) . getBaseHrefFromDOM ( ) ;
113+ const urlSerializer = envInjector . get ( UrlSerializer ) ;
114+ const locationStrategy = envInjector . get ( LocationStrategy ) ;
115+ const finalUrlSerialized = urlSerializer . serialize ( lastSuccessfulNavigation . finalUrl ) ;
116+ const finalExternalUrl = joinUrlParts ( locationStrategy . getBaseHref ( ) , finalUrlSerialized ) ;
122117
123- redirectTo = joinUrlParts ( baseHref , finalUrlStringified ) ;
118+ if ( urlToRender . href !== new URL ( finalExternalUrl , urlToRender . origin ) . href ) {
119+ redirectTo = finalExternalUrl ;
124120 }
125121 }
126122
0 commit comments