Skip to content

Commit

Permalink
feat, refactor(app): improve image loading and handling (#219)
Browse files Browse the repository at this point in the history
* feat(photo-album): wire up skeleton card
* feat(header, popover): set explicit image dimensions
* feat(blog-card): use `NgOptimizedImage`
* feat(pages): set LCP priority
* feat(componets): use `NgOptimizedImage`
* feat(blog): use `NgOptimizedImage`
* feat(talks): set LCP priority
* refactor(pages): remove debug code
* feat(components): improve loading indicator ui
* refactor(components): control spinners with signals
* refactor(components): move spinner to own component
* refactor(blog): add spinner to slug
* feat(spinner): improve positioning
* feat(index): set image dimensions
  • Loading branch information
Karvel authored Jul 13, 2024
1 parent 1ba9d30 commit cb1d9eb
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 71 deletions.
36 changes: 26 additions & 10 deletions src/app/components/blog-card/blog-card.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DatePipe, NgIf, NgStyle } from '@angular/common';
import { DatePipe, NgIf, NgOptimizedImage, NgStyle } from '@angular/common';
import {
Component,
DestroyRef,
Expand Down Expand Up @@ -29,6 +29,7 @@ import { getMonth } from '@utils/get-month';
imports: [
DatePipe,
NgIf,
NgOptimizedImage,
NgStyle,
PillComponent,
ReplaceBrokenImageDirective,
Expand Down Expand Up @@ -83,22 +84,37 @@ import { getMonth } from '@utils/get-month';
[width]="isSmallScreen ? '' : '320px'"
/>
<a [routerLink]="['/blog', year, month, post.slug]">
<img
[src]="post.attributes.cover_image"
[alt]="post.attributes.cover_image_title ?? 'Post Cover Image'"
[ngStyle]="{ visibility: showSkeleton() ? 'hidden' : 'visible' }"
(load)="onLoad()"
appReplaceBrokenImage
class="sm:max-w-xs rounded-md sm:w-full sm:h-full sm:object-cover sm:object-center"
loading="lazy"
/>
<ng-container *ngIf="isLCP; else nonPriority">
<img
[ngSrc]="post.attributes.cover_image || ''"
[alt]="post.attributes.cover_image_title ?? 'Post Cover Image'"
[ngStyle]="{ visibility: showSkeleton() ? 'hidden' : 'visible' }"
(load)="onLoad()"
appReplaceBrokenImage
class="sm:max-w-xs rounded-md sm:w-full sm:h-full sm:object-cover sm:object-center"
priority
fill
/>
</ng-container>
<ng-template #nonPriority>
<img
[ngSrc]="post.attributes.cover_image || ''"
[alt]="post.attributes.cover_image_title ?? 'Post Cover Image'"
[ngStyle]="{ visibility: showSkeleton() ? 'hidden' : 'visible' }"
(load)="onLoad()"
appReplaceBrokenImage
class="sm:max-w-xs rounded-md sm:w-full sm:h-full sm:object-cover sm:object-center"
fill
/>
</ng-template>
</a>
</div>
</div>
`,
})
export class BlogCardComponent implements OnInit {
@Input() post!: ContentFile<BlogPost>;
@Input() isLCP: boolean = false;

public isSmallScreen: boolean = false;
public month: string = '';
Expand Down
3 changes: 2 additions & 1 deletion src/app/components/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { ThemeService } from '@services/theme.service';
src="images/self/logo.png"
width="50"
alt="Hapax Legomenon logo"
loading="lazy"
height="38"
width="50"
/>
</div>
<span
Expand Down
28 changes: 14 additions & 14 deletions src/app/components/masonry-grid/masonry-grid.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AsyncPipe, NgFor, NgIf } from '@angular/common';
import { ChangeDetectorRef, Component, inject } from '@angular/core';
import { AsyncPipe, NgFor, NgIf, NgOptimizedImage } from '@angular/common';
import { Component, inject, signal, WritableSignal } from '@angular/core';

import { BehaviorSubject, tap } from 'rxjs';
import { tap } from 'rxjs';

import ImageInfoPopoverContentComponent from '@components/popover/image-info-popover-content.component';
import PopoverComponent from '@components/popover/popover.component';
import { SpinnerComponent } from '@components/spinner/spinner.component';
import { flickr } from '@constants/flickr';
import { FlickrService } from '@services/api/flickr.service';

Expand All @@ -16,18 +17,21 @@ import { FlickrService } from '@services/api/flickr.service';
ImageInfoPopoverContentComponent,
NgFor,
NgIf,
NgOptimizedImage,
PopoverComponent,
SpinnerComponent,
],
template: `
<div *ngIf="loading$ | async">Loading...</div>
<app-spinner *ngIf="loading()" class="py-3 block" />
<div *ngIf="photos$ | async as photos">
<ul class="image-gallery list-none">
<li *ngFor="let photo of photos">
<img
*ngIf="photo.url_m"
[src]="photo.url_m"
[ngSrc]="photo.url_m"
[alt]="photo.title"
loading="lazy"
height="500"
width="500"
/>
<div class="relative">
<div
Expand Down Expand Up @@ -58,15 +62,11 @@ import { FlickrService } from '@services/api/flickr.service';
styleUrls: ['./masonry-grid.component.scss'],
})
export class MasonryGridComponent {
private changeDetectorRef = inject(ChangeDetectorRef);
private flickrService = inject(FlickrService);

public flickr = flickr;
public loading$ = new BehaviorSubject<boolean>(true);
public photos$ = this.flickrService.getFavoritePhotos().pipe(
tap(() => {
this.loading$.next(false);
this.changeDetectorRef.detectChanges();
}),
);
public loading: WritableSignal<boolean> = signal(true);
public photos$ = this.flickrService
.getFavoritePhotos()
.pipe(tap(() => this.loading.set(false)));
}
61 changes: 55 additions & 6 deletions src/app/components/photo-album/photo-album.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
import { NgIf } from '@angular/common';
import { Component, Input } from '@angular/core';
import { NgIf, NgOptimizedImage, NgStyle } from '@angular/common';
import {
Component,
DestroyRef,
inject,
Input,
OnInit,
signal,
WritableSignal,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

import { Observable, debounceTime } from 'rxjs';

import { SkeletonCardComponent } from '@components/skeleton-card/skeleton-card.component';
import { flickr } from '@constants/flickr';
import { ReplaceBrokenImageDirective } from '@directives/replace-broken-image.directive';
import { PhotosetListItem } from '@models/flickr';
import { ScreenSizeService } from '@services/screen-size.service';

@Component({
selector: 'app-photo-album',
standalone: true,
imports: [NgIf, ReplaceBrokenImageDirective],
imports: [
NgIf,
NgOptimizedImage,
NgStyle,
ReplaceBrokenImageDirective,
SkeletonCardComponent,
],
template: `
<div *ngIf="photo.id" class="relative">
<app-skeleton-card
*ngIf="showSkeleton()"
class="rounded-md absolute min-w-full h-full"
height="100%"
maxWidth="100%"
[width]="isSmallScreen ? '' : '736px'"
/>
<a
[href]="flickr.albumUrl + '/' + photo.id"
target="_blank"
rel="noopener"
>
<img
[src]="
[ngSrc]="
flickr.albumPhotoUrl +
'/' +
photo.server +
Expand All @@ -27,10 +53,13 @@ import { PhotosetListItem } from '@models/flickr';
photo.secret +
'_w.jpg'
"
[ngStyle]="{ visibility: showSkeleton() ? 'hidden' : 'visible' }"
(load)="onLoad()"
alt=""
appReplaceBrokenImage
class="w-full rounded-md"
loading="lazy"
height="491"
width="736"
/>
<div
class="absolute top-0 left-0 right-0 bottom-0 flex flex-col justify-end p-4"
Expand All @@ -49,8 +78,28 @@ import { PhotosetListItem } from '@models/flickr';
</div>
`,
})
export class PhotoAlbumComponent {
export class PhotoAlbumComponent implements OnInit {
@Input() public photo!: PhotosetListItem;

public flickr = flickr;
public isSmallScreen: boolean = false;
public screenWidth$!: Observable<number>;
public showSkeleton: WritableSignal<boolean> = signal(true);

private destroyRef = inject(DestroyRef);
private screenSizeService = inject(ScreenSizeService);

public ngOnInit(): void {
const smallScreenSize = 768;
this.screenWidth$ = this.screenSizeService.screenWidth;
this.screenWidth$
.pipe(debounceTime(300), takeUntilDestroyed(this.destroyRef))
.subscribe((width) => {
this.isSmallScreen = width < smallScreenSize;
});
}

public onLoad(): void {
this.showSkeleton.set(false);
}
}
1 change: 1 addition & 0 deletions src/app/components/popover/popover.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
class="cursor-pointer drop-shadow-lg"
loading="lazy"
tabindex="0"
height="20"
width="20"
/>
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { AsyncPipe, NgFor, NgIf } from '@angular/common';
import { ChangeDetectorRef, Component, inject } from '@angular/core';
import { Component, inject, signal, WritableSignal } from '@angular/core';

import { BehaviorSubject, tap } from 'rxjs';
import { tap } from 'rxjs';

import { PhotoAlbumComponent } from '@components/photo-album/photo-album.component';
import { SpinnerComponent } from '@components/spinner/spinner.component';
import { FlickrService } from '@services/api/flickr.service';

@Component({
selector: 'app-recent-photo-albums',
standalone: true,
imports: [AsyncPipe, NgFor, NgIf, PhotoAlbumComponent],
imports: [AsyncPipe, NgFor, NgIf, PhotoAlbumComponent, SpinnerComponent],
template: `
<div *ngIf="loading$ | async">Loading...</div>
<app-spinner *ngIf="loading()" />
<div *ngIf="photos$ | async as photos">
<h2 class="text-xl">Latest Photo Albums:</h2>
<div class="flex gap-4 flex-wrap justify-center xl:justify-normal">
Expand All @@ -23,15 +24,11 @@ import { FlickrService } from '@services/api/flickr.service';
`,
})
export class RecentPhotoAlbumsComponent {
private changeDetectorRef = inject(ChangeDetectorRef);
private flickrService = inject(FlickrService);

public loading$ = new BehaviorSubject<boolean>(true);
public loading: WritableSignal<boolean> = signal(true);

public photos$ = this.flickrService.getRecentPhotosets().pipe(
tap(() => {
this.loading$.next(false);
this.changeDetectorRef.detectChanges();
}),
);
public photos$ = this.flickrService
.getRecentPhotosets()
.pipe(tap(() => this.loading.set(false)));
}
28 changes: 28 additions & 0 deletions src/app/components/spinner/spinner.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-spinner',
standalone: true,
template: `
<div role="status" class="flex justify-center">
<svg
aria-hidden="true"
class="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
`,
})
export class SpinnerComponent {}
10 changes: 6 additions & 4 deletions src/app/pages/about/index.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncPipe, NgIf } from '@angular/common';
import { AsyncPipe, NgIf, NgOptimizedImage } from '@angular/common';
import { Component, inject } from '@angular/core';
import { MetaDefinition } from '@angular/platform-browser';

Expand Down Expand Up @@ -37,17 +37,19 @@ export const metaTagList: MetaDefinition[] = [
@Component({
selector: 'app-about-index',
standalone: true,
imports: [AsyncPipe, NgIf, MarkdownComponent],
imports: [AsyncPipe, NgIf, NgOptimizedImage, MarkdownComponent],
template: `
<h1 class="sr-only">About</h1>
<div class="md:max-w md:mx-auto md:flex md:justify-center">
<div class="md:w-[48rem] p-4">
<div class="flex-1">
<img
src="images/self/me.jpg"
ngSrc="images/self/me.jpg"
class="rounded max-h-[32rem] mx-auto"
alt="Me in Norway"
loading="lazy"
priority
height="816"
width="384"
/>
<div *ngIf="about$ | async as about">
<analog-markdown
Expand Down
Loading

0 comments on commit cb1d9eb

Please sign in to comment.