Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 0978668

Browse files
committed
fix(ssr): add browser check for MatchMedia
* Add back the browser check in MatchMedia so that when the server module isn't loaded, there are no lingering window errors
1 parent cf5266a commit 0978668

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/lib/media-query/match-media.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Inject, Injectable, NgZone} from '@angular/core';
9-
import {DOCUMENT} from '@angular/common';
8+
import {Inject, Injectable, NgZone, PLATFORM_ID} from '@angular/core';
9+
import {DOCUMENT, isPlatformBrowser} from '@angular/common';
1010
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
1111
import {Observable} from 'rxjs/Observable';
1212
import {filter} from 'rxjs/operators/filter';
@@ -27,6 +27,7 @@ export class MatchMedia {
2727
protected _observable$: Observable<MediaChange>;
2828

2929
constructor(protected _zone: NgZone,
30+
@Inject(PLATFORM_ID) protected _platformId: Object,
3031
@Inject(DOCUMENT) protected _document: any) {
3132
this._registry = new Map<string, MediaQueryList>();
3233
this._source = new BehaviorSubject<MediaChange>(new MediaChange(true));
@@ -98,7 +99,8 @@ export class MatchMedia {
9899
* supports 0..n listeners for activation/deactivation
99100
*/
100101
protected _buildMQL(query: string): MediaQueryList {
101-
let canListen = !!(<any>window).matchMedia('all').addListener;
102+
let canListen = isPlatformBrowser(this._platformId) &&
103+
!!(<any>window).matchMedia('all').addListener;
102104

103105
return canListen ? (<any>window).matchMedia(query) : <MediaQueryList>{
104106
matches: query === 'all' || query === '',

src/lib/media-query/mock/mock-match-media.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Inject, Injectable, NgZone} from '@angular/core';
8+
import {Inject, Injectable, NgZone, PLATFORM_ID} from '@angular/core';
99
import {DOCUMENT} from '@angular/common';
1010

1111
import {MatchMedia} from '../match-media';
@@ -29,9 +29,11 @@ export class MockMatchMedia extends MatchMedia {
2929
useOverlaps = false;
3030

3131
constructor(_zone: NgZone,
32+
@Inject(PLATFORM_ID) _platformId: Object,
3233
@Inject(DOCUMENT) _document: any,
34+
@Inject(PLATFORM_ID) _platformId: Object,
3335
private _breakpoints: BreakPointRegistry) {
34-
super(_zone, _document);
36+
super(_zone, _platformId, _document);
3537
this._actives = [];
3638
}
3739

src/lib/media-query/server-match-media.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {DOCUMENT} from '@angular/common';
9-
import {Inject, Injectable, NgZone} from '@angular/core';
9+
import {Inject, Injectable, NgZone, PLATFORM_ID} from '@angular/core';
1010
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
1111
import {MediaChange} from './media-change';
1212
import {BreakPoint} from './breakpoints/break-point';
@@ -91,8 +91,9 @@ export class ServerMatchMedia extends MatchMedia {
9191
protected _observable$: Observable<MediaChange>;
9292

9393
constructor(protected _zone: NgZone,
94+
@Inject(PLATFORM_ID) protected _platformId: Object,
9495
@Inject(DOCUMENT) protected _document: any) {
95-
super(_zone, _document);
96+
super(_zone, _platformId, _document);
9697
this._registry = new Map<string, ServerMediaQueryList>();
9798
this._source = new BehaviorSubject<MediaChange>(new MediaChange(true));
9899
this._observable$ = this._source.asObservable();

src/lib/module.ts

-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import {MediaQueriesModule} from './media-query/_module';
1313
import {BreakPoint} from './media-query/breakpoints/break-point';
1414
import {
1515
BreakPointProviderOptions,
16-
DEFAULT_BREAKPOINTS_PROVIDER,
1716
CUSTOM_BREAKPOINTS_PROVIDER_FACTORY
1817
} from './media-query/breakpoints/break-points-provider';
19-
import {MEDIA_MONITOR_PROVIDER} from './media-query/media-monitor-provider';
20-
import {OBSERVABLE_MEDIA_PROVIDER} from './media-query/observable-media-provider';
2118

2219
import {FlexDirective} from './api/flexbox/flex';
2320
import {LayoutDirective} from './api/flexbox/layout';
@@ -70,9 +67,6 @@ const ALL_DIRECTIVES = [
7067
exports: [MediaQueriesModule, ...ALL_DIRECTIVES],
7168
declarations: [...ALL_DIRECTIVES],
7269
providers: [
73-
MEDIA_MONITOR_PROVIDER,
74-
DEFAULT_BREAKPOINTS_PROVIDER, // Extend defaults with internal custom breakpoints
75-
OBSERVABLE_MEDIA_PROVIDER,
7670
ServerStylesheet,
7771
StyleUtils,
7872
BROWSER_PROVIDER,

0 commit comments

Comments
 (0)