My angular 12 application is large and takes time to load the first time for any user. No api is triggered until all js files are loaded. If I see in Dev Tools (js enabled), You see the below screen shot. Is there anyway I can lazy load these files?
Asked
Active
Viewed 36 times
2

user2837961
- 1,505
- 3
- 27
- 67
-
Why do you have so many js files? The default config there are about 6 js files, see also https://stackoverflow.com/q/60177478/1260204. These do have cash busting hashes in the names but still only 6. How is your application structured or what are you loading that you have this many js files and also without prefixes in the name. – Igor Apr 12 '23 at 11:37
-
To manage JS files in a large Angular 12 application: use lazy loading, split code into smaller modules, use tree shaking, optimize code, use third-party libraries carefully, and consider using code splitting tools. – Umair Rasheed Apr 12 '23 at 11:39
-
These are built minified js files – user2837961 Apr 12 '23 at 11:43
-
It is hard to say because we have no access to your structure or your code. If you want more help you need to provide an [mcve] that illustrates the problem configured in the same manner your application is. – Igor Apr 12 '23 at 12:39
1 Answers
0
You need to enable lazyloading your routes in the app.routing.ts should look like this:
const appRoutes: Routes = [
{
path: 'foobar',
loadChildren: () =>
import('./foobar.module').then(m => m.FoobarModule),
}];
The module will be loaded only when the user navigates to the route.

heaxyh
- 573
- 6
- 20
-
-
My question is that after all those js files I get my apis for the home page running. Why does all js files get loaded at once? I do have loadChildren implemented – user2837961 Apr 12 '23 at 12:36
-
Could you show your routing module? And do you have circular dependencies ? – heaxyh Apr 12 '23 at 12:38
-
Thanks @heaxyh for the heads up. I had a configuration called PreloadAllModules in my routes. By removing that lazy loading works correctly – user2837961 Apr 12 '23 at 13:32