8000 feat(router) ✨ Adds Asynchrounous Redirects by MeddahAbdellah · Pull Request #60863 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

feat(router) ✨ Adds Asynchrounous Redirects #60863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally se 8000 nd you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

MeddahAbdellah
Copy link
Contributor
@MeddahAbdellah MeddahAbdellah commented Apr 14, 2025

PR Checklist

Please check if 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
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Currently the angular router only allows a string or a synchronous function for the redirectTo field of the router.

What is the new behavior?

Currently the angular router now allows also asynchronous functions that return a Promise or an Observable.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I had to change one of the tests whose assertion was unreachable for it to fail. There might be a bug in the redirection strategy. I don't know if it's done on purpose, but if the path that has the redirectTo is a subpath of the redirected to url. The redirectTo function returns it as a UrlTree, which causes an infinite loop. I have been able to reproduce it here:
https://stackblitz.com/edit/angular-vakejq-tmlfa9gy?file=src%2Fapp.routes.ts

I have added a test to lock this behaviour, but I suspect it should be modified, at least throwing an error in devMode telling the user that there might be an infinite loop. I'd gladly do it in another PR once a decision on what should be done is made. In the meantime, this PR adds async management of redirection, keeping all the current behaviours of the simple functional redirection. All tests pass locally.

@pullapprove pullapprove bot requested a review from thePunderWoman April 14, 2025 08:18
@thePunderWoman thePunderWoman requested review from atscott and removed request for thePunderWoman April 14, 2025 08:22
@ngbot ngbot bot added this to the Backlog milestone Apr 14, 2025
@thePunderWoman thePunderWoman added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews target: major This PR is targeted for the next major release labels Apr 14, 2025
@thePunderWoman
Copy link
Contributor

@MeddahAbdellah Thanks for the contribution! Looks like you have some failing tests and lint issues to fix.

@MeddahAbdellah MeddahAbdellah marked this pull request as draft April 14, 2025 08:39
@MeddahAbdellah MeddahAbdellah force-pushed the router/async-redirects branch from 4611713 to 6b0b229 Compare April 14, 2025 09:11
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: docs Related to the documentation labels Apr 14, 2025
@MeddahAbdellah
Copy link
Contributor Author

Apologies, forgot to put the PR in draft. I have updated the typo that caused the tests to fail, I'll launch them again and put the PR to review once everything is green

@MeddahAbdellah MeddahAbdellah marked this pull request as ready for review April 14, 2025 09:38
@MeddahAbdellah MeddahAbdellah marked this pull request as draft April 14, 2025 09:49
@MeddahAbdellah MeddahAbdellah marked this pull request as ready for review April 21, 2025 01:08
@MeddahAbdellah MeddahAbdellah force-pushed the router/async-redirects branch from 658dd30 to 8fa60d4 Compare April 22, 2025 12:40
@atscott atscott force-pushed the router/async-redirects branch from 8fa60d4 to 35a7b08 Compare April 22, 2025 17:11
@angular-robot angular-robot bot removed the area: docs Related to the documentation label Apr 22, 2025
@atscott atscott force-pushed the router/async-redirects branch from 35a7b08 to c289ae9 Compare April 22, 2025 17:12
@atscott atscott added requires: TGP This PR requires a passing TGP before merging is allowed 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 Apr 22, 2025
@pullapprove pullapprove bot removed the requires: TGP This PR requires a passing TGP before merging is allowed label Apr 22, 2025
@pullapprove pullapprove bot requested review from atscott and devversion April 22, 2025 17:13
@pullapprove pullapprove bot requested a review from thePunderWoman April 22, 2025 17:13
Copy link
Contributor
@atscott atscott left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

Copy link
Contributor
@thePunderWoman thePunderWoman left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

@thePunderWoman thePunderWoman added the action: merge The PR is ready for merge by the caretaker label Apr 22, 2025
@atscott atscott removed the action: merge The PR is ready for merge by the caretaker label Apr 22, 2025
@atscott
Copy link
Contributor
atscott commented Apr 22, 2025

Note to self: This should likely be marked as a breaking change

@atscott atscott force-pushed the router/async-redirects branch from c289ae9 to ba2907b Compare April 22, 2025 17:18
@atscott atscott added the action: merge The PR is ready for merge by the caretaker label Apr 22, 2025
@angular-robot angular-robot bot added the detected: breaking change PR contains a commit with a breaking change label Apr 22, 8000 2025
Adds support for asynchronous redirects in the router, allowing redirect logic to be resolved dynamically (e.g., via API or async function). This enhances routing flexibility and supports more complex navigation scenarios.

BREAKING CHANGE: The `RedirectFn` can now return `Observable` or
`Promise`. Any code that directly calls functions returning this type
may need to be adjusted to account for this.
@atscott atscott force-pushed the router/async-redirects branch from ba2907b to 5c7d909 Compare April 22, 2025 17:58
Comment on lines +200 to +214
function getRedirectResult(
redirectTo: string | RedirectFunction,
currentSnapshot: ActivatedRouteSnapshot,
injector: Injector,
): Observable<string | UrlTree> {
if (typeof redirectTo === 'string') {
return of(redirectTo);
}
const redirectToFn = redirectTo;
const {queryParams, fragment, routeConfig, url, outlet, params, data, title} = currentSnapshot;
return wrapIntoObservable(
runInInjectionContext(injector, () =>
redirectToFn({params, data, queryParams, fragment, routeConfig, url, outlet, title}),
),
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought (can be ignored): I don't know if it's better to pull it into a new function. I have had a hard time going up the stack trace when trying to debug. granted this is more elegant, I have been finding myself avoiding too much depth in my function calls (I mean by that, a function that calls a function that calls a function), it's easier to debug I find but that remains a personal opinion.

@pkozlowski-opensource
Copy link
Member

This PR was merged into the repository by commit 62de7d9.

The changes were merged into the following branches: main

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: router detected: breaking change PR contains a commit with a breaking change detected: feature PR contains a feature commit target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0