question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular 9 - Unable to discover lazy children routes

See original GitHub issue

Lazy children, am I right?

Goal:

Angular project with main route, then a sub-route with a child-router-outlet to provide a ‘sub-nav’ of sorts with lazy-loaded children

 _________________________
|      |      PARENT      |
|      |     subheader    |
| ROOT | ---------------- |
| side |                  |
| nav  |                  |
|      |       CHILD      |
|      |                  |
|      |                  |
|      |                  |
|______|__________________|

Issue:

When using router children the discovery of the routes cannot be done when the child is lazy-loaded (loadChild). Seems to work fine with directly loaded components via component: ...

Symptoms:

Given a route config like:

const routes: Routes = [
  { path: '', component: ParentComponent, children: [
    { path: 'child', component: ChildComponent },
    { path: 'lazy-child', loadChildren: () => import('./lazy-child/lazy-child.module').then(m => m.LazyChildModule) }
  ]},
];

An error/warning will be thrown from GuessJS:

Unable to extract routes from application.

And only the root will be prerendered:

Prerendering 1 route(s) to …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser CREATE …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/index.html (1232 bytes)

Expected:

Prerender the 4 routes (root, parent, child)

Prerendering 4 route(s) to …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser CREATE …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/index.html (1232 bytes) CREATE …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/child/index.html (1359 bytes) CREATE …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/lazy-child/index.html (1374 bytes) CREATE …/a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/index.html (1315 bytes)

Above expected print was given when child route was changed to:

const routes: Routes = [
  { path: '', component: ParentComponent, children: [
    { path: 'child', component: ChildComponent },
    { path: 'lazy-child', component: LazyChildComponent } // <-- Changed
  ]},
];

Reproduction:

Repo with issue: https://github.com/CharlyRipp/guessjs-a9-child-discovery-iss

Or a script that reproduces it from scratch - https://github.com/CharlyRipp/guessjs-a9-child-discovery-iss/blob/master/reproduce.sh. May be useful is seeing what’s changed from the plain project and generated files.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CharlyRippcommented, Jun 15, 2020

Many apologies – things got pretty crazy for a while, finally got around to retesting.

I updated the project (https://github.com/CharlyRipp/guessjs-a9-child-discovery-iss) to Angular 9.1.8 – universal builder 9.1.1 – guess-parser 0.4.18

guessjs-a9-child-discovery-iss(master): npm list guess-parser                                                
a9-child-discovery-iss@0.0.0 /guessjs-a9-child-discovery-iss
└─┬ @nguniversal/builders@9.1.1
  └── guess-parser@0.4.18 

And everything seems to work beautifully!

Prerendering 4 route(s) to /guessjs-a9-child-discovery-iss/dist/a9-child-discovery-iss/browser
CREATE /guessjs-a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/index.html (1079 bytes)
CREATE /guessjs-a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/child/index.html (1206 bytes)
CREATE /guessjs-a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/lazy-child/index.html (1221 bytes)
CREATE /guessjs-a9-child-discovery-iss/dist/a9-child-discovery-iss/browser/parent/index.html (1162 bytes)

The lazy-loaded module (lazy-child) is discovered and prerendered as expected!

1reaction
mgechevcommented, Feb 16, 2020

Thanks for providing the repro! I’ll try to look at it next week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Children of Lazy Loaded Modules routes don't work
You defined OrdersComponent2 as a child route to OrdersComponent : this has nothing to do with lazy loaded modules.
Read more >
Router tutorial: tour of heroes - Angular
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >
Angular Router: Child Routes, Auxiliary Routes, Master Detail
We can see that the empty path configuration would map the URL / to the component Home , and all other paths to...
Read more >
Angular Router: Children or LoadChildren?
For lazy loading. · It helps in keeping the nested routes table separate. · You must specify a routing module for loadChildren. ·...
Read more >
Manually Lazy Load Modules And Components In Angular
We subscribe to the NavigationStart events of the Angular router and if the URL includes our lazy route, we check if it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found