Skip to content

Commit 00060d3

Browse files
committed
refactor(@angular/ssr): replace logic to determine the original URL
Using `currentNavigation` might not work in all cases as this will be null when the navigate has already completed.
1 parent 58dcfd1 commit 00060d3

File tree

1 file changed

+14
-18
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+14
-18
lines changed

packages/angular/ssr/src/utils/ng.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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';
1010
import {
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';
2525
import { Console } from '../console';
26-
import { joinUrlParts, stripIndexHtmlFromURL } from './url';
26+
import { stripIndexHtmlFromURL, stripTrailingSlash } 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.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 location = envInjector.get(Location);
115+
const finalUrlSerialized = urlSerializer.serialize(lastSuccessfulNavigation.finalUrl);
116+
const finalExternalUrl = location.prepareExternalUrl(stripTrailingSlash(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

Comments
 (0)