Skip to content

Commit 84662b9

Browse files
author
Steven Orvell
committed
Fixes #3734: address HI/CE timing issue in importHref. Fixes upgrade time dependencies of scripts on previous elements in async imports.
1 parent 4b010c5 commit 84662b9

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/lib/dom-module.html‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@
9595
// loaded by this point. In addition the HTMLImports polyfill should be
9696
// changed to upgrade elements prior to running any scripts.)
9797
var cePolyfill = window.CustomElements && !CustomElements.useNative;
98+
// NOTE: Under polyfilled CE/HI, if script and html are separate, then
99+
// for dom modules to be found, script should be executed as follows:
100+
// HTMLImports.whenReady(function() {
101+
// CustomElements.ready = false;
102+
// // registrations
103+
// CustomElements.upgradeDocumentTree(document);
104+
// CustomElements.ready = true;
105+
// });
106+
// TODO(sorvell): A webcomponentsjs method should be added for this.
98107
document.registerElement('dom-module', DomModule);
99108

100109
function forceDomModulesUpgrade() {

‎src/standard/utils.html‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-->
1010

1111
<link rel="import" href="../lib/async.html">
12+
<link rel="import" href="../lib/settings.html">
1213
<link rel="import" href="../lib/debounce.html">
1314

1415
<script>
@@ -454,5 +455,38 @@
454455

455456
});
456457

458+
/*
459+
We patch importHref under the CE polyfill for 2 separate reasons:
460+
(1) performance optimization: CE registrations upgrade the entire document
461+
tree including imports. Therefore if an import is loaded every element
462+
registered will upgrade the entire doc tree. This is $ and is optimized
463+
via batching to occur once at startup (before WebComponentsReady). We
464+
override importHref here so that we can batch upgrades until after the
465+
import has loded, leveraging the same batching optimization.
466+
(2) the CE polyfill upgrades elements in HI in the wrong order. They upgrade
467+
after all scripts in the import have run rather than being interleaved with
468+
them. Therefore, any script that depends on a previous custom element in
469+
the import will fail. By deferring upgrades until after async imports load
470+
we reduce the chance of a problem because upgrade time dependencies are
471+
no longer an issue. (e.g. `dom-module` is a registration time dependency
472+
when `lazyRegister` is not used and it is specially handled in `dom-module`;
473+
`custom-style` is an upgrade time dependency native css properties are used).
474+
475+
*/
476+
if (!Polymer.Settings.useNativeCustomElements) {
477+
var importHref = Polymer.Base.importHref;
478+
Polymer.Base.importHref = function(href, onload, onerror, optAsync) {
479+
CustomElements.ready = false;
480+
var loadFn = function(e) {
481+
CustomElements.upgradeDocumentTree(document);
482+
CustomElements.ready = true;
483+
if (onload) {
484+
return onload.call(this, e);
485+
}
486+
}
487+
return importHref.call(this, href, loadFn, onerror, optAsync);
488+
}
489+
}
490+
457491
})();
458492
</script>

0 commit comments

Comments
 (0)