Skip to content

Commit ac62246

Browse files
committed
Set isBrowser as a regular function instead of a getter. Make library server-side compatible.
1 parent 6dad55c commit ac62246

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/lib/src/ngx-lazy-load-images.directive.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,42 @@ export class LazyLoadImagesDirective {
1818
intersectionObserver: IntersectionObserver;
1919
rootElement: HTMLElement;
2020

21-
constructor(element: ElementRef, public renderer: Renderer2, public ngZone: NgZone, @Inject(PLATFORM_ID) private platformId: any) {
21+
constructor(
22+
element: ElementRef,
23+
public renderer: Renderer2,
24+
public ngZone: NgZone,
25+
@Inject(PLATFORM_ID) private platformId: any) {
2226
this.rootElement = element.nativeElement;
2327
}
2428

25-
public get isBrowser(): boolean {
26-
return isPlatformBrowser(this.platformId);
27-
}
28-
2929
init() {
3030
this.registerIntersectionObserver();
31-
31+
3232
this.observeDOMChanges(this.rootElement, () => {
3333
const imagesFoundInDOM = this.getAllImagesToLazyLoad(this.rootElement);
3434
imagesFoundInDOM.forEach((image: HTMLElement) => this.intersectionObserver.observe(image));
3535
});
3636
}
37-
37+
3838
ngOnInit() {
39-
if (this.isBrowser) {
40-
require('intersection-observer');
41-
this.ngZone.runOutsideAngular(() => this.init());
39+
if (!this.isBrowser()) {
40+
return;
4241
}
43-
}
4442

43+
require('intersection-observer');
44+
this.ngZone.runOutsideAngular(() => this.init());
45+
}
46+
4547
ngOnDestroy() {
4648
if (this.intersectionObserver) {
4749
this.intersectionObserver.disconnect();
4850
}
4951
}
5052

53+
isBrowser(): boolean {
54+
return isPlatformBrowser(this.platformId);
55+
}
56+
5157
registerIntersectionObserver() {
5258
this.intersectionObserver = new IntersectionObserver(
5359
images => images.forEach(image => this.onIntersectionChange(image)),

0 commit comments

Comments
 (0)