From 1beb4438167a7f28243b990fe89ca94684175fcf Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sat, 13 Aug 2022 09:57:10 +0200 Subject: [PATCH] fix(cdk/scrolling): error during server-side rendering Fixes that an error was being thrown by the virtual scroller during server-side rendering. It wasn't failing the SSR check, because the error happens inside of a promise callback. --- src/cdk/scrolling/virtual-scroll-viewport.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cdk/scrolling/virtual-scroll-viewport.ts b/src/cdk/scrolling/virtual-scroll-viewport.ts index 77253c5db6ce..3417e8be0a78 100644 --- a/src/cdk/scrolling/virtual-scroll-viewport.ts +++ b/src/cdk/scrolling/virtual-scroll-viewport.ts @@ -13,6 +13,7 @@ import { ChangeDetectorRef, Component, ElementRef, + inject, Inject, Input, NgZone, @@ -23,6 +24,7 @@ import { ViewChild, ViewEncapsulation, } from '@angular/core'; +import {Platform} from '@angular/cdk/platform'; import { animationFrameScheduler, asapScheduler, @@ -77,6 +79,8 @@ const SCROLL_SCHEDULER = ], }) export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements OnInit, OnDestroy { + private _platform = inject(Platform); + /** Emits when the viewport is detached from a CdkVirtualForOf. */ private readonly _detachedSubject = new Subject(); @@ -205,6 +209,11 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On } override ngOnInit() { + // Scrolling depends on the element dimensions which we can't get during SSR. + if (!this._platform.isBrowser) { + return; + } + if (this.scrollable === this) { super.ngOnInit(); }