Skip to content

Commit dbdf556

Browse files
committed
fix(@angular/ssr): ensure server-side navigation triggers a redirect
When a navigation occurs on the server-side, such as using `router.navigate`, and the final URL is different from the initial URL that was requested, the server should respond with a redirect. Previously, the initial URL was being read from `router.lastSuccessfulNavigation.initialUrl`, which could be incorrect in scenarios involving server-side navigations, causing the comparison with the final URL to fail and preventing the redirect. This change ensures that the initial URL requested by the browser is used for the comparison, correctly triggering a redirect when necessary. Closes #31482
1 parent 53180a8 commit dbdf556

File tree

1 file changed

+5
-4
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+5
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
},
@@ -110,10 +110,11 @@ export async function renderAngular(
110110
} else if (lastSuccessfulNavigation?.finalUrl) {
111111
hasNavigationError = false;
112112

113-
const { finalUrl, initialUrl } = lastSuccessfulNavigation;
113+
const { finalUrl } = lastSuccessfulNavigation;
114114
const finalUrlStringified = finalUrl.toString();
115+
const initialUrl = `${urlToRender.pathname}${urlToRender.search}${urlToRender.hash}`;
115116

116-
if (initialUrl.toString() !== finalUrlStringified) {
117+
if (initialUrl !== finalUrlStringified) {
117118
const baseHref =
118119
envInjector.get(APP_BASE_HREF, null, { optional: true }) ??
119120
envInjector.get(PlatformLocation).getBaseHrefFromDOM();

0 commit comments

Comments
 (0)