Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize preconnect callbacks #23557

Merged
merged 16 commits into from
Aug 12, 2019
1 change: 1 addition & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ const forbiddenTerms = {
'src/chunk.js',
'src/inabox/amp-inabox.js',
'src/runtime.js',
'src/custom-element.js',
],
},
'AMP_CONFIG': {
Expand Down
5 changes: 3 additions & 2 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {isExperimentOn} from './experiments';
import {parseSizeList} from './size-list';
import {setStyle} from './style';
import {shouldBlockOnConsentByMeta} from '../src/consent';
import {startupChunk} from './chunk';
import {toWin} from './types';
import {tryResolve} from '../src/utils/promise';

Expand Down Expand Up @@ -584,7 +585,7 @@ function createBaseCustomElementClass(win) {
// If we do early preconnects we delay them a bit. This is kind of
// an unfortunate trade off, but it seems faster, because the DOM
// operations themselves are not free and might delay
Services.timerFor(toWin(this.ownerDocument.defaultView)).delay(() => {
startupChunk(self.document, () => {
const TAG = this.tagName;
if (!this.ownerDocument) {
dev().error(TAG, 'preconnect without ownerDocument');
Expand All @@ -594,7 +595,7 @@ function createBaseCustomElementClass(win) {
return;
}
this.implementation_.preconnectCallback(onLayout);
}, 1);
});
}
}

Expand Down
18 changes: 9 additions & 9 deletions test/unit/test-custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {ElementStub} from '../../src/element-stub';
import {LOADING_ELEMENTS_, Layout} from '../../src/layout';
import {ResourceState} from '../../src/service/resource';
import {Services} from '../../src/services';
import {chunkInstanceForTesting} from '../../src/chunk';
import {createAmpElementForTesting} from '../../src/custom-element';

describes.realWin('CustomElement', {amp: true}, env => {
Expand Down Expand Up @@ -115,6 +116,7 @@ describes.realWin('CustomElement', {amp: true}, env => {
resourcesMock = sandbox.mock(resources);
container = doc.createElement('div');
doc.body.appendChild(container);
chunkInstanceForTesting(env.ampdoc);

ElementClass = createAmpElementForTesting(win, 'amp-test', TestElement);
StubElementClass = createAmpElementForTesting(
Expand Down Expand Up @@ -734,15 +736,14 @@ describes.realWin('CustomElement', {amp: true}, env => {
return element.buildingPromise_.then(() => {
expect(element.isBuilt()).to.equal(true);
expect(testElementBuildCallback).to.be.calledOnce;
expect(testElementPreconnectCallback).to.have.not.been.called;

// Call again.
return element.build().then(() => {
expect(element.isBuilt()).to.equal(true);
expect(testElementBuildCallback).to.be.calledOnce;
expect(testElementPreconnectCallback).to.have.not.been.called;
clock.tick(1);
expect(testElementPreconnectCallback).to.be.calledOnce;
setTimeout(() => {
expect(testElementPreconnectCallback).to.be.calledOnce;
}, 0);
});
});
});
Expand Down Expand Up @@ -968,16 +969,15 @@ describes.realWin('CustomElement', {amp: true}, env => {
return element.build().then(() => {
expect(element.isBuilt()).to.equal(true);
expect(testElementLayoutCallback).to.have.not.been.called;
clock.tick(1);
expect(testElementPreconnectCallback).to.be.calledOnce;
expect(testElementPreconnectCallback.getCall(0).args[0]).to.be.false;

const p = element.layoutCallback();
expect(testElementLayoutCallback).to.be.calledOnce;
expect(testElementPreconnectCallback).to.have.callCount(2);
expect(testElementPreconnectCallback.getCall(1).args[0]).to.be.true;
expect(element.signals().get(CommonSignals.LOAD_START)).to.be.ok;
expect(element.signals().get(CommonSignals.LOAD_END)).to.be.null;
setTimeout(() => {
expect(testElementPreconnectCallback).to.have.callCount(2);
expect(testElementPreconnectCallback.getCall(1).args[0]).to.be.true;
}, 0);
return p.then(() => {
expect(element.readyState).to.equal('complete');
expect(element.signals().get(CommonSignals.LOAD_END)).to.be.ok;
Expand Down