Skip to content

Commit 79858ea

Browse files
crisbetoamysorto
authored andcommitted
fix(cdk/scrolling): error during server-side rendering (#25461)
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. (cherry picked from commit d9e96ff)
1 parent 06c2164 commit 79858ea

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/cdk/scrolling/virtual-scroll-viewport.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ChangeDetectorRef,
1414
Component,
1515
ElementRef,
16+
inject,
1617
Inject,
1718
Input,
1819
NgZone,
@@ -23,6 +24,7 @@ import {
2324
ViewChild,
2425
ViewEncapsulation,
2526
} from '@angular/core';
27+
import {Platform} from '@angular/cdk/platform';
2628
import {
2729
animationFrameScheduler,
2830
asapScheduler,
@@ -77,6 +79,8 @@ const SCROLL_SCHEDULER =
7779
],
7880
})
7981
export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements OnInit, OnDestroy {
82+
private _platform = inject(Platform);
83+
8084
/** Emits when the viewport is detached from a CdkVirtualForOf. */
8185
private readonly _detachedSubject = new Subject<void>();
8286

@@ -205,6 +209,11 @@ export class CdkVirtualScrollViewport extends CdkVirtualScrollable implements On
205209
}
206210

207211
override ngOnInit() {
212+
// Scrolling depends on the element dimensions which we can't get during SSR.
213+
if (!this._platform.isBrowser) {
214+
return;
215+
}
216+
208217
if (this.scrollable === this) {
209218
super.ngOnInit();
210219
}

0 commit comments

Comments
 (0)