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.

Prefetching doesn't work for lazy routes

See original GitHub issue

Prefetching 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:

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
th0rcommented, Sep 11, 2019

@mgechev it works now! Thanks a lot!

1reaction
mgechevcommented, Sep 10, 2019

You can try ngx-quicklink@0.1.4.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - Lazy Route gets prefetched, ignoring webpack magic ...
If I want to use Vue Router to dynamically switch between login and index (the protected view), I need to avoid login view...
Read more >
Lazy Loading Individual Vue Components and Prefetching
The purpose of lazy loading is to postpone downloading parts of your application that are not needed by the user on the initial...
Read more >
Pre-fetching lazy loaded modules produces an error while ...
I noticed an error which occurred as a result of it. Whenever i tried navigating to a different route before the notify info...
Read more >
How to make lazy loading actually work in Vue (CLI 3) - Medium
You can still make use of prefetching by using the webpackPrefetch directive, which is explained in more detail in Webpack docs, as follows:...
Read more >
Prefetch resources to speed up future navigations - web.dev
When Double-keyed HTTP cache ships, this will only work for top-level navigations and same-origin subresources, but it won't be possible to ...
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