Title: Child route guards should be evaluated **after** parent resolves · Issue #20805 · angular/angular · GitHub
Open Graph Title: Child route guards should be evaluated **after** parent resolves · Issue #20805 · angular/angular
X Title: Child route guards should be evaluated **after** parent resolves · Issue #20805 · angular/angular
Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request here...
Open Graph Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => P...
X Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request ...
Opengraph URL: https://github.com/angular/angular/issues/20805
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Child route guards should be evaluated **after** parent resolves","articleBody":"\u003c!--\r\nPLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.\r\n\r\nISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION.\r\n-->\r\n\r\n## I'm submitting a...\r\n\u003c!-- Check one of the following options with \"x\" -->\r\n\u003cpre>\u003ccode>\r\n[ ] Regression (a behavior that used to work and stopped working in a new release)\r\n[ ] Bug report \u003c!-- Please search GitHub for a similar issue or PR before submitting -->\r\n[x] Feature request\r\n[ ] Documentation issue or request\r\n[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\r\n\u003c/code>\u003c/pre>\r\n\r\n## Current behavior\r\n\u003c!-- Describe how the issue manifests. -->\r\n\r\nCurrently, child route guards have no access to parent route resolves, because resolves are being evaluated **after all guards have been dealt with no matter if these are in child or parent routes**.\r\n\r\n## Expected behavior\r\n\u003c!-- Describe what the desired behavior would be. -->\r\n\r\nChild route guards should be evaluated **after** the parent route resolves are finished.\r\n\r\n## What is the motivation / use case for changing the behavior?\r\n\u003c!-- Describe the motivation or the concrete use case. -->\r\n\r\nWhen dealing with more complicated nested route structure, current state of things results in writing unnecessary and increasingly more complicated code in deeper levels of routing.\r\n\r\nExample:\r\n\r\n- 3 kinds of entities: `Website`, `Organization`, and list of `News`\r\n- `Website` can have related `Organization`\r\n- News are only available, if `Website` has `Organization` set up, because these object are bound to both\r\n\r\nNow consider having a page with `News` detail which sits on `/website/:websiteId/news/:newsId`. As mentioned before, this can only be shown when `Website` has an `Organization` link, this means we need to add guard `WebsiteHasOrganizationGuard` on the news route level. Something like this:\r\n\r\n```\r\n{\r\n path: '',\r\n resolve: {\r\n website: WebsiteResolver,\r\n organization: WebsiteOrganizationResolver,\r\n },\r\n component: WebsiteComponent,\r\n children: [\r\n {\r\n path: 'news-detail/:news',\r\n pathMatch: 'full',\r\n canActivate: [\r\n WebsiteHasOrganizationGuard,\r\n ],\r\n children: [\r\n {\r\n path: '',\r\n canActivate: [\r\n NewsExists,\r\n ],\r\n resolve: {\r\n news: NewsResolver,\r\n },\r\n component: NewsDetailComponent,\r\n },\r\n ]\r\n }\r\n ]\r\n}\r\n```\r\n\r\nIn order to show the news page, we need to evaluate both `WebsiteHasOrganizationGuard` and `NewsExists` guard. These are run according to proposed sequence: first parent, then child. Unfortunately, **none of these guards can take advantage of parent resolves of `website` and `organization`**. Since both `NewsExists` and `WebsiteHasOrganization` guards are triggered before the `WebsiteResolver` and `WebsiteOrganizationResolver`, we are forced to mimic logic of both of them in both guards in order to successfully evaluate them.\r\n\r\n**This is against DRY and gets increasingly more complicated within deep trees of nested routes.** Quite commonly, one needs to write single-use guards/resolvers that span multiple objects that relate on each other.\r\n\r\nIf parent resolves we're evaluated **before child guards**, we can easily share the resolved `Website` and `Organization` objects in the child guards without any boilerplate code.\r\n\r\n## Environment\r\n\r\n\u003cpre>\u003ccode>\r\nAngular version: 5.0.2\r\n\u003c!-- Check whether this is still an issue in the most recent Angular version -->\r\n\r\nBrowser:\r\n- Not related to browser version\r\n","author":{"url":"https://github.com/xaralis","@type":"Person","name":"xaralis"},"datePublished":"2017-12-05T16:59:36Z","url":"https://github.com/angular/angular/issues/20805"}
| 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:91bf6240-02ae-5617-2495-a2940b5db57a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A908:10D9F8:D04C4F:11429CB:6B00F62F |
| html-safe-nonce | 2fd6042602b6b8642539e4c3ce9a095d76bcc47005b46aeb03b30364849038b4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTA4OjEwRDlGODpEMDRDNEY6MTE0MjlDQjo2QjAwRjYyRiIsInZpc2l0b3JfaWQiOiIxNjk5NDM3NjY1MTEzNDA0OTc1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1af60442419323c5dd55fe3b2f212bfd3b8fe3ca68a4f0581bcbc28d7cd91ddf |
| hovercard-subject-tag | issue:279452198 |
| 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/20805/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bec4a64771617e94de64c0e07769112d9147f67679375dce8cff0c1209d0e483/angular/angular/issues/20805 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bec4a64771617e94de64c0e07769112d9147f67679375dce8cff0c1209d0e483/angular/angular/issues/20805 |
| og:image:alt | I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x] Feature request [ ] Documentation issue or request [ ] Support request => P... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xaralis |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width