Skip to content

Commit 95c0da5

Browse files
authored
Merge pull request #6 from dimeloper/feature/further-optimize-lcp-image
Replace viewport logic with srcset utilisation.
2 parents fd4b700 + 11229b9 commit 95c0da5

4 files changed

Lines changed: 9 additions & 66 deletions

File tree

src/app/pages-optimized/pokedex-optimized/pokedex.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<!-- Main banner -->
22
<div class="banner">
33
<img
4-
[ngSrc]="banner"
4+
ngSrc="/assets/images/pokemon-banner-mobile.webp"
5+
srcset="
6+
/assets/images/pokemon-banner-mobile.webp 576w,
7+
/assets/images/pokemon-banner-mobile-2x.webp 992w,
8+
/assets/images/pokemon-banner.webp 1920w
9+
"
510
placeholder
611
fill
712
priority

src/app/pages-optimized/pokedex-optimized/pokedex.component.ts

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { Component, Inject, inject, OnInit, PLATFORM_ID } from '@angular/core';
1+
import { Component, inject } from '@angular/core';
22
import { MatButton } from '@angular/material/button';
33
import { PokemonListComponent } from '../../components/pokemon-list/pokemon-list.component';
44
import { MatDialog } from '@angular/material/dialog';
55
import { PopupComponent } from '../../components/popup/popup.component';
66
import { FormComponent } from '../../components/form/form.component';
7-
import {
8-
isPlatformBrowser,
9-
NgOptimizedImage,
10-
provideImgixLoader,
11-
} from '@angular/common';
7+
import { NgOptimizedImage, provideImgixLoader } from '@angular/common';
128
import { BannerGridComponent } from '../../components/banner-grid/banner-grid.component';
13-
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
14-
import { DeviceDetectorService } from 'ngx-device-detector';
159

1610
@Component({
1711
selector: 'app-pokedex',
@@ -29,66 +23,10 @@ import { DeviceDetectorService } from 'ngx-device-detector';
2923
provideImgixLoader('https://ng-pokedex-optimization.netlify.app/'),
3024
],
3125
})
32-
export class PokedexComponent implements OnInit {
26+
export class PokedexComponent {
3327
public hideForm = true;
3428

35-
public banners = {
36-
mobile: '/assets/images/pokemon-banner-mobile.webp',
37-
desktop: '/assets/images/pokemon-banner.webp',
38-
};
39-
// initialising the banner with a default value
40-
public banner = this.banners.mobile;
41-
42-
private breakpointObserver = inject(BreakpointObserver);
4329
private dialog = inject(MatDialog);
44-
private deviceService = inject(DeviceDetectorService);
45-
46-
private isUserAgentMobile = this.deviceService.isMobile();
47-
48-
constructor(@Inject(PLATFORM_ID) private platformId: any) {}
49-
50-
public ngOnInit() {
51-
if (isPlatformBrowser(this.platformId)) {
52-
if (!this.isUserAgentMobile) {
53-
this.banner = this.banners.desktop;
54-
}
55-
56-
// if mobile, skip the banner overwrite and the breakpoint observer logic
57-
if (this.isUserAgentMobile) {
58-
return;
59-
}
60-
61-
// if desktop we also set up breakpoint observers, so that we load the correct banners
62-
// in case the user resizes the browser window
63-
this.breakpointObserver
64-
.observe([
65-
Breakpoints.XSmall,
66-
Breakpoints.Small,
67-
Breakpoints.Medium,
68-
Breakpoints.Large,
69-
Breakpoints.XLarge,
70-
])
71-
.subscribe(result => {
72-
if (result.matches) {
73-
if (result.breakpoints[Breakpoints.XSmall]) {
74-
this.banner = this.banners.mobile;
75-
}
76-
if (result.breakpoints[Breakpoints.Small]) {
77-
this.banner = this.banners.mobile;
78-
}
79-
if (result.breakpoints[Breakpoints.Medium]) {
80-
this.banner = this.banners.desktop;
81-
}
82-
if (result.breakpoints[Breakpoints.Large]) {
83-
this.banner = this.banners.desktop;
84-
}
85-
if (result.breakpoints[Breakpoints.XLarge]) {
86-
this.banner = this.banners.desktop;
87-
}
88-
}
89-
});
90-
}
91-
}
9230

9331
openDialog(): void {
9432
const dialogRef = this.dialog.open(PopupComponent, {
54.5 KB
Loading
-31.2 KB
Loading

0 commit comments

Comments
 (0)