Skip to content

Commit

Permalink
perf(startup): optimize initial bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
pawcoding committed Mar 17, 2024
1 parent 1d34f4c commit 4b9c333
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@defer {
<rp-layout />
} @loading (after 100ms; minimum 2s) {
} @placeholder (minimum 2s) {
<rp-loading />
}
17 changes: 1 addition & 16 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import {
MobileService,
MobileServiceMock,
} from './shared/data-access/mobile.service';
import {
PaletteService,
PaletteServiceMock,
} from './shared/data-access/palette.service';
import { PwaService, PwaServiceMock } from './shared/data-access/pwa.service';
import {
ThemeService,
Expand All @@ -29,32 +25,25 @@ import {
} from './shared/data-access/version.service';

describe('AppComponent', () => {
let paletteService: PaletteServiceMock;

let app: AppComponent;
let fixture: ComponentFixture<AppComponent>;

beforeEach(async () => {
paletteService = new PaletteServiceMock();

await TestBed.configureTestingModule({
imports: [AppComponent, TranslateModule.forRoot()],
providers: [
{ provide: ActivatedRoute, useValue: {} },
// Explicit providers
{ provide: PaletteService, useValue: paletteService },
{ provide: VersionService, useClass: VersionServiceMock },
{ provide: PwaService, useClass: PwaServiceMock },
// Implicit providers from LayoutComponent
{ provide: MobileService, useClass: MobileServiceMock },
{ provide: ThemeService, useClass: ThemeServiceMock },
{ provide: AnalyticsService, useClass: AnalyticsServiceMock },
{ provide: LanguageService, useClass: LanguageServiceMock },
{ provide: PwaService, useClass: PwaServiceMock },
],
}).compileComponents();

spyOn(paletteService, 'loadPaletteFromLocalStorage');

fixture = TestBed.createComponent(AppComponent);
app = fixture.componentInstance;
fixture.detectChanges();
Expand All @@ -63,8 +52,4 @@ describe('AppComponent', () => {
it('should create the app', () => {
expect(app).toBeTruthy();
});

it('should load palette on startup', () => {
expect(paletteService.loadPaletteFromLocalStorage).toHaveBeenCalled();
});
});
11 changes: 3 additions & 8 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Component, inject } from '@angular/core';
import { LayoutComponent } from './layout/layout.component';
import { LoadingComponent } from './loading/loading.component';
import { PaletteService } from './shared/data-access/palette.service';
import { PwaService } from './shared/data-access/pwa.service';
import { VersionService } from './shared/data-access/version.service';

@Component({
selector: 'rp-root',
Expand All @@ -11,10 +10,6 @@ import { PwaService } from './shared/data-access/pwa.service';
templateUrl: './app.component.html',
})
export class AppComponent {
private readonly _paletteService = inject(PaletteService);
private readonly _pwaService = inject(PwaService);

constructor() {
this._paletteService.loadPaletteFromLocalStorage();
}
// This service gets injected here to print the version number in the console on startup.
private readonly _versionService = inject(VersionService);
}
2 changes: 2 additions & 0 deletions src/app/layout/layout.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MobileService,
MobileServiceMock,
} from '../shared/data-access/mobile.service';
import { PwaService, PwaServiceMock } from '../shared/data-access/pwa.service';
import {
ThemeService,
ThemeServiceMock,
Expand All @@ -38,6 +39,7 @@ describe('LayoutComponent', () => {
{ provide: LanguageService, useValue: languageService },
{ provide: MobileService, useClass: MobileServiceMock },
{ provide: AnalyticsService, useClass: AnalyticsServiceMock },
{ provide: PwaService, useClass: PwaServiceMock },
],
}).compileComponents();

Expand Down
3 changes: 3 additions & 0 deletions src/app/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '../shared/data-access/analytics.service';
import { LanguageService } from '../shared/data-access/language.service';
import { MobileService } from '../shared/data-access/mobile.service';
import { PwaService } from '../shared/data-access/pwa.service';
import { ThemeService } from '../shared/data-access/theme.service';
import { Theme } from '../shared/types/theme';
import { sleep } from '../shared/utils/sleep';
Expand Down Expand Up @@ -52,6 +53,8 @@ export class LayoutComponent implements AfterViewInit {
private readonly _languageService = inject(LanguageService);
private readonly _themeService = inject(ThemeService);
private readonly _analyticsService = inject(AnalyticsService);
// This service gets injected into the layout to initialize the PWA service after the layout has been initialized.
private readonly _pwaService = inject(PwaService);

private readonly _header =
viewChild.required<ElementRef<HTMLElement>>('header');
Expand Down
2 changes: 2 additions & 0 deletions src/app/shared/data-access/palette.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class PaletteService {
}

constructor() {
this.loadPaletteFromLocalStorage();

effect(() => {
this._updateVariables();
});
Expand Down

0 comments on commit 4b9c333

Please sign in to comment.