@@ -33,8 +33,8 @@ export class ViewportRuler implements OnDestroy {
3333 /** Subscription to streams that invalidate the cached viewport dimensions. */
3434 private _invalidateCache : Subscription ;
3535
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 ( ( ) => {
3838 return merge < Event > ( fromEvent ( window , 'resize' ) , fromEvent ( window , 'orientationchange' ) ) ;
3939 } ) : observableOf ( ) ;
4040
@@ -51,7 +51,14 @@ export class ViewportRuler implements OnDestroy {
5151 this . _updateViewportSize ( ) ;
5252 }
5353
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 ;
5562 }
5663
5764 /** Gets a ClientRect for the viewport's bounds. */
@@ -80,6 +87,12 @@ export class ViewportRuler implements OnDestroy {
8087
8188 /** Gets the (top, left) scroll position of the viewport. */
8289 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+
8396 // The top-left-corner of the viewport is determined by the scroll position of the document
8497 // body, normally just (scrollLeft, scrollTop). However, Chrome and Firefox disagree about
8598 // whether `document.body` or `document.documentElement` is the scrolled element, so reading
@@ -107,7 +120,9 @@ export class ViewportRuler implements OnDestroy {
107120
108121 /** Updates the cached viewport size. */
109122 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 } ;
111126 }
112127}
113128
0 commit comments