Skip to content

Commit

Permalink
feat(slick): hide all slides except first one when server side rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
leo6104 committed Jan 7, 2023
1 parent c200060 commit ff42022
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/slick.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
import { isPlatformServer } from '@angular/common';
import {
AfterViewChecked,
AfterViewInit,
Expand All @@ -16,6 +16,7 @@ import {
OnInit,
Output,
PLATFORM_ID,
Renderer2,
SimpleChanges
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
Expand Down Expand Up @@ -241,20 +242,23 @@ export class SlickCarouselComponent implements OnDestroy, OnChanges, AfterViewIn
selector: '[ngxSlickItem]',
})
export class SlickItemDirective implements OnInit, OnDestroy {
constructor(public el: ElementRef,
@Inject(PLATFORM_ID) private platformId: string,
@Host() private carousel: SlickCarouselComponent) {
constructor(
public el: ElementRef,
public renderer: Renderer2,
@Inject(PLATFORM_ID) private platformId: string,
@Host() private carousel: SlickCarouselComponent,
) {
}

ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
this.carousel.addSlide(this);
this.carousel.addSlide(this);
if (isPlatformServer(this.platformId) && this.carousel.slides.length > 0) {
// Do not show other slides in server side rendering (broken ui can be affacted to Core Web Vitals)
this.renderer.setStyle(this.el, 'display', 'none');
}
}

ngOnDestroy() {
if (isPlatformBrowser(this.platformId)) {
this.carousel.removeSlide(this);
}
this.carousel.removeSlide(this);
}
}

0 comments on commit ff42022

Please sign in to comment.