0

I have successfully implemented lazy loading for my modules Products and Customers, and I am able to see the chunks when I navigate to respective routing.

Now I want the above modules to preload, which can be done simply by adding

{ preloadingStrategy: PreloadAllModules }

But this is not working, The above modules are still loading lazily (chunks are loading when I navigate to respective modules instead of initial load).


This is how Implemented

app.module.ts

@NgModule({
 imports: [LazyLoadModule]
})

lazy-load.module.ts

@NgModule({
    imports: [RouterModule.forRoot([{
        path: 'products',
        data: {
            preload: false
        },
        loadChildren: './entities/products/products.module#ProductsModule',
    },
    {
        path: 'customers',
        data: {
            preload: true
        },
        loadChildren: './entities/customers/customers.module#CustomersModule',
    }], { useHash: true, preloadingStrategy: PreloadAllModules })]
})

However if I add the configuration directly in app.module.ts, then it seems to work(I can see the individual chunks at initial load), But my routing paths are not working.


Update

First I tried with custom preloading strategy like this

preloading-strategy.service.ts

@Injectable()
export class PreloadingStrategyService implements PreloadingStrategy {

    preload(route: Route, load: () => Observable<any>) {
        console.log('Preloading called');
        return route.data && route.data.preload ? load() : of(null);
    }
}

lazy-load.module.ts

{ preloadingStrategy: PreloadingStrategyService }

But preload method is never called(No console log printed), So for testing purpose I added PreloadAllModules. This also doesn't work as I already explained above.

Sameer
  • 4,758
  • 3
  • 20
  • 41
  • can you provide a minimal stackblitz? – Aakash Garg Jun 01 '20 at 09:18
  • @AakashGarg I tried reproducing it in standalone project, but no issue there(works fine). The issue is with my project. – Sameer Jun 01 '20 at 09:23
  • in lazy module shouldn't you be using forChild instead of forRoot? I can also see preload flag as false. in any other module you are using any preloading strategy? – Aakash Garg Jun 01 '20 at 09:23
  • @AakashGarg Yes my end goal is to create **custom preloading strategy** service, But that doesn't work(never called/stopped in debugger). So I added **PreloadAllModules**. Yes I used `forChild` in **Products** and **Customers** Module – Sameer Jun 01 '20 at 09:29
  • https://coryrylan.com/blog/custom-preloading-and-lazy-loading-strategies-with-angular – Aakash Garg Jun 01 '20 at 09:35
  • @AakashGarg I have gone through the above article and many others (doesn't work), I have updated my question – Sameer Jun 01 '20 at 09:47
  • add RouterModule in exports of Lazyloadmodule ngmodule decorator. – Aakash Garg Jun 01 '20 at 09:50
  • check below how i exported routing module. – Aakash Garg Jun 01 '20 at 09:53

2 Answers2

0

Change your lazy-load.module.ts to

@NgModule({
    imports: [RouterModule.forRoot([{
        path: 'products',
        data: {
            preload: false
        },
        loadChildren: './entities/products/products.module#ProductsModule',
    },
    {
        path: 'customers',
        data: {
            preload: true
        },
        loadChildren: './entities/customers/customers.module#CustomersModule',
    }], { useHash: true, preloadingStrategy: PreloadAllModules })],
    exports: [RoutingModule]
})
Aakash Garg
  • 10,649
  • 2
  • 7
  • 25
  • You mean `RouterModule` ?. Added that to exports but still modules are loading lazily(chunks are loading when navigated for both modules). – Sameer Jun 01 '20 at 10:00
  • you need to provide a proper stackblitz. can't find actual problem area like this. – Aakash Garg Jun 01 '20 at 10:30
0

The issue is caused by module: "commonjs" in tsconfig.json, It should be module: "ESNext".

Found here

Sameer
  • 4,758
  • 3
  • 20
  • 41