Prefetching doesn't work for lazy routes
See original GitHub issuePrefetching doesn’t work for me when I load a lazy route with links to other lazy routes.
Here is my config:
@NgModule({
imports: [SharedModule, AppRoutingModule]
})
export class AppModule {}
@NgModule({
imports: [
RouterModule.forRoot(routes, {
onSameUrlNavigation: 'reload',
relativeLinkResolution: 'corrected',
preloadingStrategy: QuicklinkStrategy
})
],
exports: [RouterModule]
})
export class AppRoutingModule {}
@NgModule({
imports: [CommonModule, RouterModule, QuicklinkModule],
exports: [CommonModule, RouterModule, QuicklinkModule]
})
export class SharedModule {}
@NgModule({
imports: [
SharedModule,
RouterModule.forChild(lazyModuleRoutes)
]
})
export class LazyModule {}
I tried to debug the problem and it appeared that PrefetchRegistry#shouldPrefetch
method is being called with proper urls but this.trees
array is empty here:
https://github.com/mgechev/ngx-quicklink/blob/d7bd54c0c8b34822efee208b8631a39fc268d436/src/prefetch-registry.service.ts#L20-L23
I added a few console.logs after that:
-
Added
console.log("created a registry");
to the constructor ofPrefetchRegistry
: https://github.com/mgechev/ngx-quicklink/blob/d7bd54c0c8b34822efee208b8631a39fc268d436/src/prefetch-registry.service.ts#L12-L14 -
Added
console.log("added a tree", tree.toString(), 'this.trees.length =', this.trees.length);
to thePrefetchRegistry#add
method: https://github.com/mgechev/ngx-quicklink/blob/d7bd54c0c8b34822efee208b8631a39fc268d436/src/prefetch-registry.service.ts#L16-L18
And here are the results:
created a registry
created a registry
added a tree /path-to-lazy-route-on-screen this.trees.length = 1
should prefetch called for /path-to-lazy-route-on-screen this.trees.length = 0
Note that PrefetchRegistry
was created twice and seems like shouldPrefetch
method was called on the wrong instance with empty trees
array.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
@mgechev it works now! Thanks a lot!
You can try
ngx-quicklink@0.1.4
.