@@ -33,8 +33,8 @@ export class ViewportRuler implements OnDestroy {
33
33
/** Subscription to streams that invalidate the cached viewport dimensions. */
34
34
private _invalidateCache : Subscription ;
35
35
36
- constructor ( platform : Platform , ngZone : NgZone ) {
37
- this . _change = platform . isBrowser ? ngZone . runOutsideAngular ( ( ) => {
36
+ constructor ( private _platform : Platform , ngZone : NgZone ) {
37
+ this . _change = _platform . isBrowser ? ngZone . runOutsideAngular ( ( ) => {
38
38
return merge < Event > ( fromEvent ( window , 'resize' ) , fromEvent ( window , 'orientationchange' ) ) ;
39
39
} ) : observableOf ( ) ;
40
40
@@ -51,7 +51,14 @@ export class ViewportRuler implements OnDestroy {
51
51
this . _updateViewportSize ( ) ;
52
52
}
53
53
54
- return { width : this . _viewportSize . width , height : this . _viewportSize . height } ;
54
+ const output = { width : this . _viewportSize . width , height : this . _viewportSize . height } ;
55
+
56
+ // If we're not on a browser, don't cache the size since it'll be mocked out anyway.
57
+ if ( ! this . _platform . isBrowser ) {
58
+ this . _viewportSize = null ! ;
59
+ }
60
+
61
+ return output ;
55
62
}
56
63
57
64
/** Gets a ClientRect for the viewport's bounds. */
@@ -80,6 +87,12 @@ export class ViewportRuler implements OnDestroy {
80
87
81
88
/** Gets the (top, left) scroll position of the viewport. */
82
89
getViewportScrollPosition ( ) {
90
+ // While we can get a reference to the fake document
91
+ // during SSR, it doesn't have getBoundingClientRect.
92
+ if ( ! this . _platform . isBrowser ) {
93
+ return { top : 0 , left : 0 } ;
94
+ }
95
+
83
96
// The top-left-corner of the viewport is determined by the scroll position of the document
84
97
// body, normally just (scrollLeft, scrollTop). However, Chrome and Firefox disagree about
85
98
// whether `document.body` or `document.documentElement` is the scrolled element, so reading
@@ -107,7 +120,9 @@ export class ViewportRuler implements OnDestroy {
107
120
108
121
/** Updates the cached viewport size. */
109
122
private _updateViewportSize ( ) {
110
- this . _viewportSize = { width : window . innerWidth , height : window . innerHeight } ;
123
+ this . _viewportSize = this . _platform . isBrowser ?
124
+ { width : window . innerWidth , height : window . innerHeight } :
125
+ { width : 0 , height : 0 } ;
111
126
}
112
127
}
113
128
0 commit comments