I recently launched my site with Angular and made it universal. But when I check this site on the gtmetrix, it does not good performance. Can anyone guide me what to do? I drastically reduced the size of the photos but have no idea about JavaScript files.
Link Gtmetrix: https://gtmetrix.com/reports/tarhbama.com/4vp1UBUI/
I always use ng build --prod. I use gzipper files.
app.module.ts
import { BrowserModule, BrowserTransferStateModule, makeStateKey, TransferState } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { environment } from 'src/environments/environment';
import { ServiceWorkerModule } from '@angular/service-worker';
import { SharedModule } from './shared/shared.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
BrowserAnimationsModule,
BrowserTransferStateModule,
CoreModule,
AppRoutingModule,
SharedModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './core/services/auth-guard.service';
import { AdminContentComponent } from './shared/layout/admin-content/admin-content.component';
import { UiContentComponent } from './shared/layout/ui-content/ui-content.component';
const routes: Routes = [
{ path: 'admin', canActivate: [AuthGuard], component: AdminContentComponent, loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) },
{ path: '', component: UiContentComponent, loadChildren: () => import('./ui/ui.module').then(m => m.UiModule) },
];
@NgModule({
imports: [RouterModule.forRoot(routes, {
initialNavigation: 'enabled',
scrollPositionRestoration: 'enabled',
onSameUrlNavigation: 'reload',
})],
exports: [RouterModule]
})
export class AppRoutingModule { }
Here are few ways to reduce the angular bundle size.
ng build --prod
. This --prod flag will perform aot and tree shaking which will reduce the bundle size.(for angular v5 or greater). For versions less than that, you will have to useng build --prod --build-optimizer
Node js
Java
If your bundle size does get too big you may want to analyze your bundle because you may have used an inappropriate large-sized third party package or you forgot to remove some package if you are not using it anymore. Webpack has an amazing feature to give us a visual idea of the composition of a webpack bundle.
Steps to get the graph:
1.npm install -g webpack-bundle-analyzer
In your Angular app, run ng build --stats-json (don’t use flag --prod). By enabling --stats-json you will get an additional file stats.json
Finally, run webpack-bundle-analyzer path/to/your/stats.json and your browser will pop up the page at localhost:8888. Have fun with it.
You may be surprised,
a) that you forgot to remove some packages you are not using anymore and/or
b) that some packages are way larger than expected and could be replaced with another one and/or
c) that you have improperly imported some libraries (For example, 80% of moment.js is just locale data which is probably not needed) so that you have some direction to look for an answer.
Split your module into smaller chunks and load only the ones which are required.
Using Angular Universal A.K.A. server-side rendering (not in cli)
Web Workers (again, not in cli, but a very requested feature) see: https://github.com/angular/angular-cli/issues/2305
Service Workers see: https://github.com/angular/angular-cli/issues/4006
References:
https://medium.com/angular-in-depth/optimize-angular-bundle-size-in-4-steps-4a3b3737bf45
How to decrease prod bundle size?