Skip to content

Commit 6639085

Browse files
committed
refactor(@angular/ssr): simplify redirect URL determination
The logic to determine the final URL for server-side rendering redirects is simplified. Previously, it used LocationStrategy and UrlSerializer to construct the final URL. This is replaced by using PlatformLocation to directly get the pathname, search, and hash. This change removes unnecessary complexity and dependencies, making the code easier to understand and maintain.
1 parent b1d6d2f commit 6639085

File tree

1 file changed

+8
-11
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+8
-11
lines changed

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

Lines changed: 8 additions & 11 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 { LocationStrategy } from '@angular/common';
9+
import { PlatformLocation } 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, UrlSerializer } from '@angular/router';
24+
import { ActivatedRoute, Router } 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.
@@ -107,16 +107,13 @@ export async function renderAngular(
107107

108108
if (!routerIsProvided) {
109109
hasNavigationError = false;
110-
} else if (lastSuccessfulNavigation?.finalUrl) {
110+
} else if (lastSuccessfulNavigation) {
111111
hasNavigationError = false;
112+
const { pathname, search, hash } = envInjector.get(PlatformLocation);
113+
const finalUrl = [stripTrailingSlash(pathname), search, hash].join('');
112114

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);
117-
118-
if (urlToRender.href !== new URL(finalExternalUrl, urlToRender.origin).href) {
119-
redirectTo = finalExternalUrl;
115+
if (urlToRender.href !== new URL(finalUrl, urlToRender.origin).href) {
116+
redirectTo = finalUrl;
120117
}
121118
}
122119

0 commit comments

Comments
 (0)