66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { APP_BASE_HREF , PlatformLocation } from '@angular/common' ;
9+ import { Location } from '@angular/common' ;
1010import {
1111 ApplicationRef ,
1212 type PlatformRef ,
@@ -21,9 +21,9 @@ 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' ;
26- import { joinUrlParts , stripIndexHtmlFromURL } from './url' ;
26+ import { stripIndexHtmlFromURL } from './url' ;
2727
2828/**
2929 * Represents the bootstrap mechanism for an Angular application.
@@ -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 . toString ( ) ,
6969 document : html ,
7070 } ,
7171 } ,
@@ -96,31 +96,26 @@ 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 finalUrl = lastSuccessfulNavigation . finalUrl ;
114+ const finalUrlSerialized = envInjector . get ( UrlSerializer ) . serialize ( finalUrl ) ;
115+ const finalExternalUrl = envInjector . get ( Location ) . prepareExternalUrl ( finalUrlSerialized ) ;
122116
123- redirectTo = joinUrlParts ( baseHref , finalUrlStringified ) ;
117+ if ( urlToRender . href !== new URL ( finalExternalUrl , urlToRender . origin ) . href ) {
118+ redirectTo = finalExternalUrl ;
124119 }
125120 }
126121
0 commit comments