0

Edit: still haven't solved, I'm using angular 9 rc7 btw

Many hours passed and I still don't understand whats wrong, thats my repo where you can check directly - https://github.com/Mautriz/angular-boilerplate/tree/feature/cypress

I have a root router, a lazy-loaded "pages" module, and the pages module has many lazy loaded children

// App
const routes: Routes = [
  {
    path: "page-not-found",
    component: NotFoundComponent
  },
  {
    path: "",
    loadChildren: () => import("./pages/pages.module").then(m => m.PagesModule),
    data: { preload: true }
  },

  {
    path: "**",
    redirectTo: "page-not-found"
  }
];
// Pages
const routes: Routes = [
  {
    path: "",
    component: LayoutComponent,
    children: [
      {
        path: "statistics",
        loadChildren: () =>
          import("./statistics/statistics.module").then(
            m => m.StatisticsModule
          ),
        data: { preload: true }
      },
      {
        path: "",
        loadChildren: () =>
          import("./homepage/homepage.module").then(m => m.HomepageModule),
        data: { preload: true }
      }
    ]
  }
];

The problem is that if I check chromes request, I can only see the "pages-module" lazily loaded, but can't see the single pages ones (even tho they work)

Why are they not lazily loaded ? Am I missing something? I'm trying to implement a custom strategy and that's preventing me from understanding if it's working or not

EDIT: https://i.sstatic.net/G56zx.png The submodules are not getting generated appearently, and I don't know why

2
  • While serving the app, do you see the chunks getting generated for the single pages ones? Commented Dec 22, 2019 at 10:16
  • Just checked, no they aren't getting generated :/ i.imgur.com/AbW6v7j.png Commented Dec 22, 2019 at 10:18

2 Answers 2

1

You seem to not have configured the HomePageRoutingModule and StatisticsRoutingModule. Hence the issue.

Please configure those modules as well to see chunks of them getting generated.

Here is how you would do that for HomepageRoutingModule:

import { HomepageComponent } from "./homepage.component";
import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";

const routes: Routes = [{ path: "", component: HomepageComponent }];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomepageRoutingModule {}

Similarly for StatisticsRoutingModule, you'd do it like this:

import { StatisticsComponent } from "./statistics.component";
import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";

const routes: Routes = [
  {
    path: "",
    component: StatisticsComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class StatisticsRoutingModule {}

Seems to be getting generated for me:

enter image description here

Here's the GitHub Repo for the Code that I have for your ref.

Here's the Sample Working Code Demo for your ref.

Sign up to request clarification or add additional context in comments.

4 Comments

PS: You won't see the modules getting downloaded on lazily and on-demand on StackBlitz coz they have some really complex algorithm that preloads even the lazily-loaded modules. In order to see it lazily-load modules on-demand, you'll have to check by cloning the repo that I've attached with the answer. Just letting you know about it upfront :)
hey man, thanks for the answer, I already had those modules configured with router and forChild tho, that's the repository github.com/Mautriz/angular-boilerplate/tree/feature/cypress. Don't mind all the random stuff, I'm using this project to learn
I think these additional changes that you've made is what's breaking the code chunking. I also see that you've used some CustomPreloadStrategy as your preloadStrategy in the AppRoutingModule. That could be causing the issue. BTW, I was not able to run your App on my local as I kept getting core.js:6406 ERROR Error: Uncaught (in promise): Error: Runtime compiler is not loaded error
It worked to me using both npm and yarn on a different computer (having just downloaded it), have you used the ng serve as "npm run ng serve"? might be because you are using a different angular version (i'm using 9.0.0-rc7
0

I solved the problem, it was just a simple importing mistake in the end.
I had imported the homepage module in the pages module (even tho the lazy loading in routing was correct)

@NgModule({
  imports: [
    CommonModule,
    PagesRoutingModule,
    HomePageModule, // MISTAKE!!!
    SharedModule,
    TranslateModule.forChild({ loader: TranslationLoader })
  ],
  declarations: [LayoutComponent]
})
export class PagesModule {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.