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.

How to lazy load with chunks

See original GitHub issue

Hello,

I’ve been using ui-router since AngularJS, but I’ve never really felt the need to lazy-load modules until now, as performance is a major point for a project I’m working on. I’ve come into contact with the lazy-load mechanism of the default Angular Router before, but generally don’t like the default router, so I’d prefer to stay with ui-router.

My problem is that with the ui-router lazy loading, Angular doesn’t seem to generate separate chunks for lazy loaded modules, resulting in no size difference of the initial chunk. I’ve been following the guide for Angular (https://ui-router.github.io/guide/lazyloading#angular) and adjusted my code accordingly (see example module below).

media-player.module.ts

...
@NgModule({ 
  declarations: [...],
  imports: [
    SharedModule,
    UIRouterModule.forChild({
      states: [{
        name: 'mediaPlayer',
        url: '/player/:id',
        component: MediaPlayerComponent,
      }],
    }),
  ];
})
export class MediaPlayerModule { }

app.module.ts

...
@NgModule({
  declarations: [...],
  imports: [
    UIRouterModule.forRoot({
      states: [
        ...,
        {
          name: 'mediaPlayer.**',
          loadChildren: async () => import('./modules/media-player/media-player.module').then((m) => m.MediaPlayerModule),
        },
        ...,
      ],
      initial: ...,
      otherwise: ...,
      config: ...,
    }),
  ],
  providers: [...],
  bootstrap: [AppComponent],
})
export class AppModule {}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
Lubjancommented, Sep 28, 2021

Okay will do that, thank you very much for your help. Hope you have a pleasant Tuesday 😃

1reaction
mokipediacommented, Sep 28, 2021

As the module holds the state definitions, I think that is normal. You can however (and I would encourage you to) split the lazyloaded routes each into a future state to minimize the chunk size and give the future state a full url description. that way the url is always known, even when the state itselt if not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack lazy-loading chunks - CodeX Team
Webpack supports splitting files to separate modules out of a box. This note will show you how to start using Webpack chunks and...
Read more >
Lazy Loading | webpack
Lazy, or "on demand", loading is a great way to optimize your site or application. This practice essentially involves splitting your code at...
Read more >
Lazy loading in Webpack | Uploadcare Blog
Learn about Lazy Loading in Webpack with Igor Adamenko! ... Entry chunks contain modules we set as entry points in webpack configuration.
Read more >
Common Chunk and Lazy Loading in Angular - | juri.dev
The easiest way of applying lazy loading is to use the Angular Router's built-in functionality. const routes: Routes = [ { path: ' ......
Read more >
A Guide to React Lazy Loading - In Plain English
By default, code split chunks are named by the index in which they are created. In the above example, LazyLoad is contained within...
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