Skip to content

fix(@angular/ssr): resolve relative redirects against the parent route - #34301

Merged
alan-agius4 merged 2 commits into
angular:mainfrom
bschaeublin:fix/ssr-relative-redirect-base
Oct 12, 2026
Merged

alan-agius4 merged 2 commits into
angular:mainfrom
bschaeublin:fix/ssr-relative-redirect-base

Conversation

@bschaeublin

Copy link
Copy Markdown
Contributor

Fixes #34300

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #34300

A relative redirectTo is resolved on the server by removing only the last segment of the full route path. For a route whose own path has several segments, { path: 'old/page', redirectTo: 'new/page' } redirects /old/page to /old/new/page, while the router navigates to /new/page, as it replaces all segments matched by the route.

What is the new behavior?

Relative redirects are resolved against the parent route path, which traverseRoutesConfig already passes down. This matches the router for multi-segment paths, empty paths and catch-all routes, including prerendered routes with getPrerenderParams, where the parameters are substituted into the parent path (previously a ** expanded to several segments shifted the base, e.g. docs/** with a/b redirected to /docs/a/new instead of /new).

Single-segment and empty-path routes resolve exactly as before.

Does this PR introduce a breaking change?

  • Yes
  • No

This changes the redirect target only for relative redirects on routes whose own path has more than one segment. The previous target was never a URL the router would navigate to, so this is treated as a fix. The two app_spec.ts expectations that asserted the previous result (redirect/relative → /redirect/home, redirect/:param/relative → /redirect/param/home) now expect the router's result /home. If you consider this breaking, I'm happy to adjust.

Other information

Tests: new cases in ng-routes_spec.ts (multi-segment path under a parameter, prerendered multi-segment path with getPrerenderParams, prerendered ** with getPrerenderParams) and app_spec.ts (relative redirect under a parameterized parent route through app.handle). Verified locally with //packages/angular/ssr/test:test, the tests/build/server-rendering e2e tests (esbuild, Node 24) and ng-dev release build.

Not covered by this change: positional parameters in redirectTo itself (redirectTo: 'profile/:id') are still emitted literally, as before.

Related: #34299 (query string on relative redirects, #34298). Both change the end of resolveRedirectTo, so whichever lands second needs a small rebase.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Angular SSR routing logic to resolve relative redirects against the parent route instead of the current route path, aligning with Angular router behavior. It introduces a parentRoute parameter to routing handlers and updates resolveRedirectTo accordingly, accompanied by comprehensive unit tests. The review feedback points out that if parentRoute contains a catch-all wildcard (**), it will not be resolved correctly because only URL parameters are being replaced; it suggests also replacing CATCH_ALL_REGEXP for consistency.

Comment thread packages/angular/ssr/src/routes/ng-routes.ts
@bschaeublin

Copy link
Copy Markdown
Contributor Author

While working on this, I noticed a related gap. On the server, parameters written in the redirect target itself aren't filled in, so they end up in the URL as literal text:

Route Request Server Location Router navigates to
user/:id → profile/:id /user/7 /profile/:id /profile/7
search/:term → /find?q=:x /search/cats?x=dogs /find?q=:x /find?q=dogs

It's independent of this PR and #34299, but a fix would touch the same code, so I didn't want to open a third PR right away. Happy to send it as a follow-up once these land, if you'd like.

@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Oct 12, 2026
Comment thread packages/angular/ssr/test/app_spec.ts Outdated
@alan-agius4 alan-agius4 added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Oct 12, 2026
When a route's own path has several segments, such as `old/page`, only its last segment was
replaced when resolving a relative `redirectTo` on the server. A redirect to `new/page` was
therefore sent to `/old/new/page`, while the router navigates to `/new/page` because it replaces
all segments matched by the route. The server now does the same.

This changes the redirect target of relative redirects on routes whose path has more than one
segment.

Fixes angular#34300
@bschaeublin
bschaeublin force-pushed the fix/ssr-relative-redirect-base branch from 935c49c to e9bfb52 Compare October 12, 2026 10:05
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Oct 12, 2026

@alan-agius4 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alan-agius4
alan-agius4 merged commit b3bbada into angular:main Oct 12, 2026
64 of 68 checks passed
@alan-agius4

Copy link
Copy Markdown
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/ssr target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSR resolves relative redirectTo against the wrong base when the route path has several segments

2 participants