Skip to content

Commit 4c850c7

Browse files
committed
fix: delay on-load sanity checks
Delays the sanity checks from the `CompatibilityModule` by 5 seconds, in order to give the user's base styles a better chance to load. Fixes #4125.
1 parent 8d0cd04 commit 4c850c7

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/lib/core/compatibility/compatibility.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Optional,
88
isDevMode,
99
ElementRef,
10+
NgZone,
1011
} from '@angular/core';
1112
import {DOCUMENT} from '@angular/platform-browser';
1213
import {MdError} from '../errors/error';
@@ -193,16 +194,21 @@ export class CompatibilityModule {
193194
};
194195
}
195196

196-
constructor(@Optional() @Inject(DOCUMENT) private _document: any) {
197-
if (!hasDoneGlobalChecks && isDevMode()) {
198-
this._checkDoctype();
199-
this._checkTheme();
200-
hasDoneGlobalChecks = true;
197+
constructor(@Optional() @Inject(DOCUMENT) private _document: any, ngZone: NgZone) {
198+
if (!hasDoneGlobalChecks && _document && isDevMode()) {
199+
ngZone.runOutsideAngular(() => {
200+
// Delay running the check to allow more time for the user's styles to load.
201+
setTimeout(() => {
202+
this._checkDoctype();
203+
this._checkTheme();
204+
hasDoneGlobalChecks = true;
205+
}, 5000);
206+
});
201207
}
202208
}
203209

204210
private _checkDoctype(): void {
205-
if (this._document && !this._document.doctype) {
211+
if (!this._document.doctype) {
206212
console.warn(
207213
'Current document does not have a doctype. This may cause ' +
208214
'some Angular Material components not to behave as expected.'
@@ -211,7 +217,7 @@ export class CompatibilityModule {
211217
}
212218

213219
private _checkTheme(): void {
214-
if (this._document && typeof getComputedStyle === 'function') {
220+
if (typeof getComputedStyle === 'function') {
215221
const testElement = this._document.createElement('div');
216222

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

0 commit comments

Comments
 (0)