diff --git a/build-system/tasks/presubmit-checks.js b/build-system/tasks/presubmit-checks.js index ddba4a1cfcf3..4f081544057f 100644 --- a/build-system/tasks/presubmit-checks.js +++ b/build-system/tasks/presubmit-checks.js @@ -424,6 +424,12 @@ var forbiddenTerms = { 'src/log.js', ], }, + '\\.schedulePass\\(': { + message: 'schedulePass is heavy, thinking twice before using it', + whitelist: [ + 'src/service/resources-impl.js', + ], + }, '(win|Win)(dow)?(\\(\\))?\\.open\\W': { message: 'Use dom.openWindowDialog', whitelist: [ diff --git a/src/render-delaying-services.js b/src/render-delaying-services.js index ae3b7d466f2f..e2203f5b3e3a 100644 --- a/src/render-delaying-services.js +++ b/src/render-delaying-services.js @@ -41,7 +41,8 @@ const LOAD_TIMEOUT = 3000; * Detects any render delaying services that are required on the page, * and returns a promise with a timeout. * @param {!Window} win - * @return {!Promise} + * @return {!Promise>} resolves to an Array that has the same length as + * the detected render delaying services */ export function waitForServices(win) { const promises = includedServices(win).map(service => { diff --git a/src/service/resources-impl.js b/src/service/resources-impl.js index db19bad9972c..bc81791559a7 100644 --- a/src/service/resources-impl.js +++ b/src/service/resources-impl.js @@ -695,9 +695,13 @@ export class Resources { /** * Schedules the work pass at the latest with the specified delay. * @param {number=} opt_delay + * @param {boolean=} opt_relayoutAll * @return {boolean} */ - schedulePass(opt_delay) { + schedulePass(opt_delay, opt_relayoutAll) { + if (opt_relayoutAll) { + this.relayoutAll_ = true; + } return this.pass_.schedule(opt_delay); } diff --git a/src/style-installer.js b/src/style-installer.js index d92b81126307..440e1672cac8 100644 --- a/src/style-installer.js +++ b/src/style-installer.js @@ -20,6 +20,7 @@ import {performanceFor} from './performance'; import {platformFor} from './platform'; import {setStyles} from './style'; import {waitForServices} from './render-delaying-services'; +import {resourcesForDoc} from './resources'; const bodyVisibleSentinel = '__AMP_BODY_VISIBLE'; @@ -128,14 +129,17 @@ export function makeBodyVisible(doc, opt_waitForServices) { } }; const win = doc.defaultView; - const docState = documentStateFor(win); - docState.onBodyAvailable(() => { + documentStateFor(win).onBodyAvailable(() => { if (win[bodyVisibleSentinel]) { return; } win[bodyVisibleSentinel] = true; if (opt_waitForServices) { - waitForServices(win).then(set, set).then(() => { + waitForServices(win).catch(() => []).then(services => { + set(); + if (services.length > 0) { + resourcesForDoc(doc)./*OK*/schedulePass(1, /* relayoutAll */ true); + } try { const perf = performanceFor(win); perf.tick('mbv'); diff --git a/test/functional/test-render-delaying-services.js b/test/functional/test-render-delaying-services.js index a6888d5595fd..1a267f416c78 100644 --- a/test/functional/test-render-delaying-services.js +++ b/test/functional/test-render-delaying-services.js @@ -51,7 +51,7 @@ describe('waitForServices', () => { it('should resolve if no blocking services is presented', () => { //