Title: Angular Router state committed before activation can make a valid error-handler redirect loop forever · Issue #71126 · angular/angular · GitHub
Open Graph Title: Angular Router state committed before activation can make a valid error-handler redirect loop forever · Issue #71126 · angular/angular
X Title: Angular Router state committed before activation can make a valid error-handler redirect loop forever · Issue #71126 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? router Is this a regression? No Description An app that sends navigation failures to an error page the documented way never finishes the redirect: withNavigationErrorHandler(() => ne...
Open Graph Description: Which @angular/* package(s) are the source of the bug? router Is this a regression? No Description An app that sends navigation failures to an error page the documented way never finishes the redir...
X Description: Which @angular/* package(s) are the source of the bug? router Is this a regression? No Description An app that sends navigation failures to an error page the documented way never finishes the redir...
Opengraph URL: https://github.com/angular/angular/issues/71126
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Angular Router state committed before activation can make a valid error-handler redirect loop forever","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nrouter\n\n### Is this a regression?\n\nNo\n\n### Description\n\nAn app that sends navigation failures to an error page the documented way never finishes the redirect:\n\n```ts\nwithNavigationErrorHandler(() => new RedirectCommand(inject(Router).parseUrl('/error')));\n```\n\n`/error` renders fine on its own. But `GET /(//)` makes the SSR worker stop answering everything, while it spins the event loop.\n\nIt comes down to an ordering problem. `commitTransition()` runs on `BeforeActivateRoutes`, before the browser URL is written:\n\n```ts\nelse if (e instanceof BeforeActivateRoutes) {\n this.commitTransition(currentTransition);\n if (this.urlUpdateStrategy === 'deferred' && !currentTransition.extras.skipLocationChange) {\n this.setBrowserUrl(this.createBrowserPath(currentTransition), currentTransition);\n }\n}\n```\n\n`ActivatedRoute.snapshot` is only assigned later, by `advanceActivatedRoute()` during activation. So a throw in between leaves a committed `routerState` whose routes have no snapshot.\n\nNormally that gets repaired, because `NavigationError` calls `restoreHistory(t, true)`. A `RedirectCommand` takes the other branch instead, and that branch is excluded:\n\n```ts\nelse if (e instanceof NavigationCancel && !isRedirectingEvent(e)) {\n this.restoreHistory(currentTransition);\n}\n```\n\nSo the redirect runs with the half-committed state as its previous state, and `DefaultRouteReuseStrategy` reads it unguarded:\n\n```ts\nshouldReuseRoute(future, curr) {\n return future.routeConfig === curr.routeConfig; // curr is undefined\n}\n```\n\nThat throws, the handler answers with another `RedirectCommand`, and it never ends.\n\nThe URL is not the only way into that window. `ActivateRoutes.activate()` advances one route at a time, and the component is created in between:\n\n```ts\nactivateRoutes(futureNode, currNode, parentContexts) {\n advanceActivatedRoute(future); // this route gets its snapshot\n // ...then context.outlet.activateWith() creates the component, and that is what throws\n}\n```\n\nWith one outlet the route is already advanced when the component throws, so nothing is left behind. With two, the primary advances, its component throws, and the sibling outlet never advances. Same half-committed state, no odd URL involved.\n\n### Minimal Reproduction\n\n```ts\n@Component({selector: 'app-login', template: '\u003cp>LOGIN-OK\u003c/p>'})\nexport class Login {\n // guards pass returnUrl around encoded, so it gets decoded again here\n protected readonly returnUrl = decodeURIComponent(\n inject(ActivatedRoute).snapshot.queryParamMap.get('returnUrl') ?? '/',\n );\n}\n\nexport const routes: Routes = [\n { path: 'shell', component: Shell },\n { path: 'error', component: ErrorPage },\n // control: an ordinary resolver failure, no commit-window throw\n {\n path: 'boom',\n component: Shell,\n resolve: {\n x: () => {\n throw new Error('nope');\n },\n },\n },\n { path: 'login', component: Login },\n { path: 'room/:id', outlet: 'chat', component: Chat },\n { path: '**', component: Shell },\n];\n```\n\n`\u003crouter-outlet />\u003crouter-outlet name=\"chat\" />` in the shell, `\u003cbase href=\"/\">` and `urlUpdateStrategy: 'deferred'` left at their defaults.\n\nCase A, the browser URL write fails:\n\n```text\nlocalhost:4000/boom # 302, recovers in one hop\nlocalhost:4000/(//) # Never answers, spins the event loop\n```\n\n`/boom` is the control: it hits the same handler and the same `/error`, and recovers in one hop. `/(//)` never answers.\n\nCase B, a component fails next to a named outlet. `?returnUrl=%25` is a correctly encoded `%`, so the URL itself is valid. The Router hands the component a decoded value already, so `decodeURIComponent` gets `%` and throws `URIError: URI malformed`:\n\n```text\nlocalhost:4000/login?returnUrl=%25 # 302, throws and recovers in one hop\nlocalhost:4000/login(chat:room/42)?returnUrl=%25 # Never answers\n```\n\nThe first line is the control. Same throw, one hop, worker fine. The only difference on the second is `(chat:room/42)`, and that comes from the request, not from the configuration.\n\nThe error chain, written synchronously because nothing async flushes once the loop starts:\n\n```text\nERRCHAIN #1 nope \u003c- /boom, stops here\nERRCHAIN #2 NG05701: // \u003c- /(//)\nERRCHAIN #3 Cannot read properties of undefined (reading 'routeConfig')\nERRCHAIN #4 Cannot read properties of undefined (reading 'routeConfig')\n...\n```\n\nCase B produces the same chain, with `URI malformed` in place of `NG05701`.\n\n### Please provide a link to a minimal reproduction of the bug\n\nSee https://github.com/SkyZeroZx/angular-ssr-router-redirect-loop\n\n### Anything else?\n\n\nI narrowed this down to `RedirectCommand`. With the same setup, no handler or a handler returning `undefined` gives a normal 404, and `router.navigate('/error')` is noisy but settles. Only returning a `RedirectCommand` never completes, through either `withNavigationErrorHandler()` or `RouterModule.forRoot({errorHandler})`.\n\nThe setup is otherwise default. Case A needs the `**` route, because without it `/(//)` fails before the router commits anything. Case B needs the named outlet in the shell template instead, and the unactivated route has to be a sibling rather than a descendant.\n\n#71094 fixes the `/(//)` trigger by preventing `DefaultUrlSerializer` from producing protocol relative paths (reported at #69700), but it only removes one way into the window above. \n\nCase B still reproduces with it applied. A try/catch around `setBrowserUrl()` has the same gap: it handles one source of the throw, but the state may already be committed.\n\nWhat covers both is restoring the internal state when a redirecting navigation is canceled, but only when that transition actually committed (`this.routerState === targetRouterState`). \n\nThe browser URL does not need restoring, since the redirect replaces it. Adding `curr?.routeConfig` in `shouldReuseRoute()` stops the crash but leaves the state inconsistent.\n","author":{"url":"https://github.com/SkyZeroZx","@type":"Person","name":"SkyZeroZx"},"datePublished":"2026-10-01T19:01:27Z","url":"https://github.com/angular/angular/issues/71126"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:10bd4264-6a06-19fc-04ff-4d1fee5b1549 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A50A:1558B:3B0B2F:53FE36:6AFDE46B |
| html-safe-nonce | 4b69f4ab73c1f0ff68aa774deb3c6ff79f1d4509ef5c09c8e3e72409257b2115 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTBBOjE1NThCOjNCMEIyRjo1M0ZFMzY6NkFGREU0NkIiLCJ2aXNpdG9yX2lkIjoiNjQzMDUzNjEwOTUxNTQ2NTgzNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2848799a67202b046b3d5c2f91f16b407a02e46d9820eb0fb00d6ef797737215 |
| hovercard-subject-tag | issue:5667561426 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/71126/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a912dbba3f610ee6daad95e8f28bb98608482b77ab91d4316089cf92bd8ddc0/angular/angular/issues/71126 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a912dbba3f610ee6daad95e8f28bb98608482b77ab91d4316089cf92bd8ddc0/angular/angular/issues/71126 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? router Is this a regression? No Description An app that sends navigation failures to an error page the documented way never finishes the redir... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SkyZeroZx |
| hostname | github.com |
| expected-hostname | github.com |
| None | 91ef1a291a064ac31ac459ab3f75261dbdd924c41ea5bd027e591c5b46cc9ce7 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d62bc98ce2346f4006e4a2810e9e22f37e7e6422 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width