Skip to content

Commit fb9ea53

Browse files
devversionmmalerba
authored andcommitted
fix: safety check for window in common module. (#8816)
* Currently if someone renders his Angular application using server side rendering, the `MatCommonModule` will throw an error due to missing `window` variable checks. Fixes #8809
1 parent 0c53312 commit fb9ea53

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/lib/core/common-behaviors/common-module.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export class MatCommonModule {
3737
/** Reference to the global `document` object. */
3838
private _document = typeof document === 'object' && document ? document : null;
3939

40+
/** Reference to the global 'window' object. */
41+
private _window = typeof window === 'object' && window ? window : null;
42+
4043
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean) {
4144
if (this._areChecksEnabled() && !this._hasDoneGlobalChecks) {
4245
this._checkDoctypeIsDefined();
@@ -52,7 +55,7 @@ export class MatCommonModule {
5255

5356
/** Whether the code is running in tests. */
5457
private _isTestEnv() {
55-
return window['__karma__'] || window['jasmine'];
58+
return this._window && (this._window['__karma__'] || this._window['jasmine']);
5659
}
5760

5861
private _checkDoctypeIsDefined(): void {
@@ -90,7 +93,11 @@ export class MatCommonModule {
9093

9194
/** Checks whether HammerJS is available. */
9295
_checkHammerIsAvailable(): void {
93-
if (this._areChecksEnabled() && !this._hasCheckedHammer && !window['Hammer']) {
96+
if (this._hasCheckedHammer || !this._window) {
97+
return;
98+
}
99+
100+
if (this._areChecksEnabled() && !this._window['Hammer']) {
94101
console.warn(
95102
'Could not find HammerJS. Certain Angular Material components may not work correctly.');
96103
}

src/universal-app/prerender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {enableProdMode} from '@angular/core';
21
import {renderModuleFactory} from '@angular/platform-server';
32
import {readFileSync, writeFileSync} from 'fs-extra';
43
import {log} from 'gulp-util';
@@ -7,7 +6,8 @@ import 'reflect-metadata';
76
import 'zone.js';
87
import {KitchenSinkServerModuleNgFactory} from './kitchen-sink/kitchen-sink.ngfactory';
98

10-
enableProdMode();
9+
// Do not enable production mode, because otherwise the `MatCommonModule` won't execute
10+
// the browser related checks that could cause NodeJS issues.
1111

1212
const result = renderModuleFactory(KitchenSinkServerModuleNgFactory, {
1313
document: readFileSync(join(__dirname, 'index.html'), 'utf-8')

0 commit comments

Comments
 (0)