@@ -4,18 +4,19 @@ import {
44 AfterViewInit ,
55 ChangeDetectionStrategy ,
66 Component ,
7+ effect ,
78 ElementRef ,
9+ inject ,
810 Injector ,
11+ input ,
912 OnDestroy ,
1013 PLATFORM_ID ,
11- Renderer2 ,
12- effect ,
13- inject ,
14- input ,
15- untracked ,
14+ Renderer2
1615} from '@angular/core' ;
1716import { getZoneUnPatchedApi } from './internal/get-zone-unpatched-api' ;
1817import { SvgRegistry } from './svg-registry.service' ;
18+ import { toObservable , toSignal } from '@angular/core/rxjs-interop' ;
19+ import { of , switchMap } from 'rxjs' ;
1920
2021/**
2122 * getZoneUnPatchedApi
@@ -110,6 +111,10 @@ export class FastSvgComponent implements AfterViewInit, OnDestroy {
110111 width = input < string > ( '' ) ;
111112 height = input < string > ( '' ) ;
112113
114+ #url = toSignal ( toObservable ( this . name ) . pipe ( switchMap ( ( name ) => {
115+ return this . registry . url ( name ) ;
116+ } ) ) )
117+
113118 // When the browser loaded the svg resource we trigger the caching mechanism
114119 // re-fetch -> cache-hit -> get SVG -> cache in DOM
115120 loadedListener = ( ) => {
@@ -142,7 +147,6 @@ export class FastSvgComponent implements AfterViewInit, OnDestroy {
142147 ( onCleanup ) => {
143148 const name = this . name ( ) ;
144149
145- untracked ( ( ) => {
146150 if ( ! name ) {
147151 throw new Error ( 'svg component needs a name to operate' ) ;
148152 }
@@ -153,32 +157,35 @@ export class FastSvgComponent implements AfterViewInit, OnDestroy {
153157 if ( ! this . registry . isSvgCached ( name ) ) {
154158 /**
155159 CSR - Browser native lazy loading hack
156-
160+
157161 We use an img element here to leverage the browsers native features:
158162 - lazy loading (loading="lazy") to only load the svg that are actually visible
159163 - priority hints to down prioritize the fetch to avoid delaying the LCP
160-
164+
161165 While the SVG is loading we display a fallback SVG.
162166 After the image is loaded we remove it from the DOM. (IMG load event)
163167 When the new svg arrives we append it to the template.
164-
168+
165169 Note:
166170 - the image is styled with display none. this prevents any loading of the resource ever.
167171 on component bootstrap we decide what we want to do. when we remove display none it performs the browser native behavior
168- - the image has 0 height and with and containment as well as contnet -visibility to reduce any performance impact
169-
170-
172+ - the image has 0 height and with and containment as well as content -visibility to reduce any performance impact
173+
174+
171175 Edge cases:
172176 - only resources that are not loaded in the current session of the browser will get lazy loaded (same URL to trigger loading is not possible)
173177 - already loaded resources will get emitted from the cache immediately, even if loading is set to lazy :o
174178 - the image needs to have display other than none
175179 */
176- const i = this . getImg ( this . registry . url ( name ) ) ;
177- this . renderer . appendChild ( this . element . nativeElement , i ) ;
178-
179- // get img
180- img = elem . querySelector ( 'img' ) as HTMLImageElement ;
181- addEventListener ( img , 'load' , this . loadedListener ) ;
180+ const url = this . #url( ) ;
181+ if ( url ) {
182+ const i = this . getImg ( url ) ;
183+ this . renderer . appendChild ( this . element . nativeElement , i ) ;
184+
185+ // get img
186+ img = elem . querySelector ( 'img' ) as HTMLImageElement ;
187+ addEventListener ( img , 'load' , this . loadedListener ) ;
188+ }
182189 }
183190
184191 // Listen to svg changes
@@ -225,7 +232,6 @@ export class FastSvgComponent implements AfterViewInit, OnDestroy {
225232 img . removeEventListener ( 'load' , this . loadedListener ) ;
226233 }
227234 } ) ;
228- } ) ;
229235 } ,
230236 { injector : this . injector }
231237 ) ;
0 commit comments