Skip to content

Commit

Permalink
fix(common): don't log doctype warning when rendering server-side (#6833
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
crisbeto authored and andrewseguin committed Sep 29, 2017
1 parent 7a354a0 commit f8ed442
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/lib/core/common-behaviors/common-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

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

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

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

if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) sanityChecksEnabled: boolean) {
if (sanityChecksEnabled && !this._hasDoneGlobalChecks && isDevMode()) {
this._checkDoctype();
this._checkTheme();
this._hasDoneGlobalChecks = true;
}
}

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

private _checkTheme(): void {
if (typeof getComputedStyle === 'function') {
if (this._document && typeof getComputedStyle === 'function') {
const testElement = this._document.createElement('div');

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

0 comments on commit f8ed442

Please sign in to comment.