Skip to content

Commit f8ed442

Browse files
crisbetoandrewseguin
authored andcommitted
fix(common): don't log doctype warning when rendering server-side (#6833)
Prevents the doctype warning from being logged when rendering server-side in development mode. Previously, while we did have a `document`, it was not the same as the client-side `document` and thus didn't have a `doctype`. We didn't notice this earlier, because we run the server-side rendering check in production mode where the sanity checks are disabled. Relates to #6292.
1 parent 7a354a0 commit f8ed442

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {NgModule, InjectionToken, Optional, Inject, isDevMode} from '@angular/core';
10-
import {DOCUMENT} from '@angular/platform-browser';
1110
import {BidiModule} from '@angular/cdk/bidi';
1211
import {CompatibilityModule} from '../compatibility/compatibility';
1312

@@ -33,19 +32,19 @@ export class MatCommonModule {
3332
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
3433
private _hasDoneGlobalChecks = false;
3534

36-
constructor(
37-
@Optional() @Inject(DOCUMENT) private _document: any,
38-
@Optional() @Inject(MATERIAL_SANITY_CHECKS) _sanityChecksEnabled: boolean) {
35+
/** Reference to the global `document` object. */
36+
private _document = typeof document === 'object' && document ? document : null;
3937

40-
if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
38+
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) sanityChecksEnabled: boolean) {
39+
if (sanityChecksEnabled && !this._hasDoneGlobalChecks && isDevMode()) {
4140
this._checkDoctype();
4241
this._checkTheme();
4342
this._hasDoneGlobalChecks = true;
4443
}
4544
}
4645

4746
private _checkDoctype(): void {
48-
if (!this._document.doctype) {
47+
if (this._document && !this._document.doctype) {
4948
console.warn(
5049
'Current document does not have a doctype. This may cause ' +
5150
'some Angular Material components not to behave as expected.'
@@ -54,7 +53,7 @@ export class MatCommonModule {
5453
}
5554

5655
private _checkTheme(): void {
57-
if (typeof getComputedStyle === 'function') {
56+
if (this._document && typeof getComputedStyle === 'function') {
5857
const testElement = this._document.createElement('div');
5958

6059
testElement.classList.add('mat-theme-loaded-marker');

0 commit comments

Comments
 (0)