Title: NavigationExtras not evaluated on NavigationCancel for initial Route · Issue #16796 · angular/angular · GitHub
Open Graph Title: NavigationExtras not evaluated on NavigationCancel for initial Route · Issue #16796 · angular/angular
X Title: NavigationExtras not evaluated on NavigationCancel for initial Route · Issue #16796 · angular/angular
Description: I'm submitting a ... (check one with "x") [x] bug report => search github for a similar issue or PR before submitting [ ] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/an...
Open Graph Description: I'm submitting a ... (check one with "x") [x] bug report => search github for a similar issue or PR before submitting [ ] feature request [ ] support request => Please do not submit support request...
X Description: I'm submitting a ... (check one with "x") [x] bug report => search github for a similar issue or PR before submitting [ ] feature request [ ] support request => Please do not su...
Opengraph URL: https://github.com/angular/angular/issues/16796
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NavigationExtras not evaluated on NavigationCancel for initial Route","articleBody":"\u003c!--\nIF YOU DON'T FILL OUT THE FOLLOWING INFORMATION WE MIGHT CLOSE YOUR ISSUE WITHOUT INVESTIGATING\n-->\n\n**I'm submitting a ...** (check one with \"x\")\n```\n[x] bug report => search github for a similar issue or PR before submitting\n[ ] feature request\n[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\n```\n\n**Current behavior**\n\u003c!-- Describe how the bug manifests. -->\n\nIn the case where the Route contains `{path: '', redirectTo: '/home'}`:\n\n1. Navigate to a guarded url from a new window so that the app is loaded for the first time to this url\n2. Have the Guard deny the route activation\n3. The Router will then navigate back to the previous route which is defaulted to `/` when initialised\n4. No Router Parts will be evaluated on the new location of `/` such as `redirectTo` and instead of being redirected to `/home` and loading that page, it will be blank as no components are specified for `/`\n\nThis isn't an issue if the app has already been loaded in the tab as the router will have already redirected from `/` to `/home` and `/home` will be `currentUrlTree` for which the Router returns to.\n\n**Expected behavior**\n\u003c!-- Describe what the behavior would be without the bug. -->\nAt step `4.` above, evaulate the router parts for the route.\n\n**Minimal reproduction of the problem with instructions**\n\u003c!--\nIf the current behavior is a bug or you can illustrate your feature request better with an example, \nplease provide the *STEPS TO REPRODUCE* and if possible a *MINIMAL DEMO* of the problem via\nhttps://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5).\n-->\n\nHave routes setup:\n```typescript\nconst routes: Routes = [\n {path: '' redirectTo: '/home', pathMatch: 'full'},\n {path: 'home', component: HomeComponent},\n {path: 'guardedendpoint', component: GuardedEndpointComponent, canActivate: [EndpointGuard]}, \n];\n```\nand the `EndpointGuard.canActivate` returns `false`\n\n1. Navigate browser to `/#/guardedendpoint` in a new tab (so that the app loads to this url, rather than navigating to it)\n2. `EndpointGuard` will deny the naviation, so a `NavigationCancel` event will fire from `Router.events`\n3. `Router` will [`resetUrlToCurrentUrlTree`](https://github.com/angular/angular/blob/master/packages/router/src/router.ts#L729) to the [`rawUrlTree`](https://github.com/angular/angular/blob/master/packages/router/src/router.ts#L275-L276) which is [initialised](https://github.com/angular/angular/blob/master/packages/router/src/url_tree.ts#L13) to the root path of `/`\n4. `Router` will then navigate to this route but won't check the `Routes` configuration (because it assumes it was already checked and parsed when it was initially navigated to though in this case, it wasn't) for the `redirectTo` parameter and so will display an empty page and won't redirect\n\n**What is the motivation / use case for changing the behavior?**\n\u003c!-- Describe the motivation or the concrete use case -->\nRouter will now follow any redirects (or other Router Parts) on the root path instead of ignoring them\n\nThe work around I am using is to `subscribe` to `Router.events` `Subject` and if the navigation is cancelled (`Router/NavigationCancel`) is emitted with an `id` of `1` (where it is the first route), I am queuing naviation to `/` so that the router will then know to `redirectTo` using the following code\n\n```typescript\nexport class AppModule {\n constructor(router: Router){\n var routerEvents$ = router.events.subscribe((message) => {\n if(message.id === 1 && message instanceof NavigationCancel){ //if first navigation and it is cancelled\n router.navigate(['']); //nav to '' so that redirects will be applied!\n routerEvents$.unsubscribe();\n } else if(message.id > 1) routerEvents$.unsubscribe(); \n });\n }\n}\n```\n\n* **Angular Router version:** 3.4.8\n\u003c!-- Check whether this is still an issue in the most recent Angular version -->\n\n* **Language:** TypeScript 2.2","author":{"url":"https://github.com/GusGold","@type":"Person","name":"GusGold"},"datePublished":"2017-05-15T13:45:26Z","url":"https://github.com/angular/angular/issues/16796"}
| 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:494cbcd3-1c18-85c5-69d6-0818f9c95374 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3A6:25A204:DE2B8B:125961B:6B00FB9A |
| html-safe-nonce | 3891069f5cbeba2968f31cbcf965b48c277cc413556b9f47a1db7a675e9ccd36 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0E2OjI1QTIwNDpERTJCOEI6MTI1OTYxQjo2QjAwRkI5QSIsInZpc2l0b3JfaWQiOiI4OTg5OTQ0NTg2MjAxNzkxMzg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 494000fb4d0c42195862def02d9f2960461ca82eb7fcdf512c924e1e87f1e785 |
| hovercard-subject-tag | issue:228719888 |
| 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/16796/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5835b8687c83ea0ca7d02fa7634079142cc44ddceed4aef6221783c31f90ddab/angular/angular/issues/16796 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5835b8687c83ea0ca7d02fa7634079142cc44ddceed4aef6221783c31f90ddab/angular/angular/issues/16796 |
| og:image:alt | I'm submitting a ... (check one with "x") [x] bug report => search github for a similar issue or PR before submitting [ ] feature request [ ] support request => Please do not submit support request... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GusGold |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width