HTMLImport polyfill is extremely slow in IE11
I've noticed, that in IE11 HTMLImport polyfill increases a delay before WebComponentsReady event is fired.
Even if there is no <link rel='import'> tag on the page.
In my case if i remove HTMLImport code from webcomponents-lite.js then delay drops from ~5s to ~1s.
I've also noticed that HTMLImport polyfill is very sensitive to the amount of other html/ajax request on the page. For example, if page has google adsense or any banners network, then delay can be up to ~10-20s!
So far my solution is to create cutom build of webcomponents polyfill without HTMLImport. But maybe there is more elegant solution to disable HTMLImport polyfill?
Nothing should be taking that long. I suspect there's something else going on. Can you post a jsbin that repros the issue?
Hi @ebidel, here is a simple example to reproduce the issue https://jsfiddle.net/wxncd3f1/
If you run it in IE11 you will see that WebComponentsReady event is fired only after all images are loaded. If you open Network tab in Developers tools it would be clear that event is fired only after all pending http requests are resolved.
On my machine with 60 images I see the delay ~ 8seconds.
And there is direct correlation between amount of http request and delay before WebComponentsReady event.
For Firefox it is not the case, WebComponentsReady event is fired immediately after DomContentLoaded.
@ebidel and here is example with custom build of webcomponents-lite.js without polyfill for HTMLImport - https://jsfiddle.net/y5v7p3n0/3/
As you can see WebComponentsReady event is fired almost immediately (~80 ms on my machine).
I'm no expert in the poyfills, but I believe this is due to your demo setup.
The difference between native HTML imports and polyfilling them is that the polyfill needs to script! It sets up a MutationObserver to detect imports on the page and waits for the document to be "complete" before doing its thing.
In your demo, you're running a bunch of script at the bottom of the page that appends images to the DOM on page load. Scripts block parsing. This in turn, delays the polyfill from firing the HTMLImportsLoaded and WebComponentsReady. It also artificially delays DOMContentLoaded and load events!
For example, in Edge, using 100 images delays all the events:
If you delay-load those images, I think you'll see a big difference :) For example, wrap all your code in an setTimeout(function() { ... }, 1000); and see the difference in WebComponentsReady firing.
BTW, in general I would use HTMLImportsLoaded instead of WebComponentsReady. It'll fire faster.
Hi @ebidel, you just confirmed my assumption that the reason for delay is exactly the waiting for all sub resources http requests to be resolved, because only after that document.readyState would be complete.
Unfortunately on the real website it is not possible to delay all images, scripts, etc to be loaded after some timeout. For example, image tags are rendered server-side, not frontend-side, but delay is still there, example: https://jsfiddle.net/wxncd3f1/1/
Maybe the solution would be to detect imports earlier, not waiting when document.readyState is complete, but when it is interactive?
In this example it is clear that interactive event occurs much earlier then complete https://jsfiddle.net/wxncd3f1/3/
Is there a good reason why only for IE polyfill is wating for the complete?
BTW, using HTMLImportsLoaded instead of WebComponentsReady won't help, as they are fired almost simultaneously, here you can see it https://jsfiddle.net/wxncd3f1/4/
SSR images are fine and that's what you should be doing. In the case of https://jsfiddle.net/wxncd3f1/1/, there's no blocking scripts so WCR fires at 20-25ms for me on IE11 and Edge in a VM. Not sure why you're seeing larger numbers.
One trick to lazily-load the images is to not have a src by default and use JS post-load to add the src attribute. For example:
<img data-src="https://example.com/image.jpg">
<img data-src="https://example.org/image.jpg">
<!-- Post load complete -->
<script>
document.querySelectorAll('img[data-src]').forEach(node => {
node.src = node.dataset.src;
});
</script>
This could alleviate the "not possible to delay image loading" issue to work-around the performance bottleneck being observed.
@ebidel if you clean the cache then SSR images would also give you delay.
@ebidel , @Garbee I think you are trying to fix the issue in the wrong way. The problem is that WebComponentsReady event is fired in IE only after all sub-resource http requests are resolved. So any slow http requests can prevent webcomponent to be rendered.
And this problem doesn't exist in FF or Safari!
For existing website it is not feasible to change all http request (not only for images, but also for scripts, ajax requests, etc) to be executed after HTMLImport polyfill is executed. So now it is almost impossible to use webcomponents polyfill in IE for the websites that load lots of resources.
So far i see two possible solutions:
- Preferred. Use for IE the same event as for other browsers -
document.readyState === 'interactive'instead ofdocument.readyState === 'complete'here - Workaround. Create a new build of webcomponents polyfill without
HTMLImport, then it doesn't affect load time in IE (here is an example https://jsfiddle.net/y5v7p3n0/3/, compare with https://jsfiddle.net/y5v7p3n0)
I am going to implement the first approach and share the results.
@ebidel, here is demo for the fix i've proposed above https://jsfiddle.net/y5v7p3n0/4/.
For all browsers html imports are executed when document.readyState === 'interactive'
I've also tested this fix with the Polymer components and didn't notice any bugs. But of course it makes sense to run more extensive testing.
Do you know - maybe there was some reason not to use document.readyState === 'interactive' for IE?
Not sure. @kevinpschaaf @sorvell @azakus @garlicnation may know the timing details.
I've look through the code and found this comment about IE and document.readyState === 'interactive'
// When loading at readyState interactive time, bootstrap only if HTMLImports // are not pending. Also avoid IE as the semantics of this state are unreliable.
Maybe there is some information why readyState interactive is unreliable in IE? Maybe it is not the case for IE11?
Probably related to:
- https://social.msdn.microsoft.com/forums/ie/en-us/6981866f-b548-42e4-a259-a66a5cafb380/ie11-onreadystatechange-event-not-firing
- https://connect.microsoft.com/IE/feedback/details/792880/document-readystat
- http://stackoverflow.com/questions/26834845/in-ie11-event-is-not-firing-in-javascript
Looks like it could be an issue with older versions of IE that need to be supported.
Hi @ebidel, looks like 1st and 3rd links are related only to iframe issue and not relevant for HTMLImport. Please correct if I am wrong, but webcomponents polyfill doesn't use iframes.
The issue described in 2nd link sounds to be resolved in IE11 and from my experience this is correct. Though, i didn't try in IE10.
If polyfill needs to support IE10 and this makes IE11 run slower - i would do more accurate browser detection and for IE11 use document.readyState === 'interactive'.
What do you think?
Yes, I think that'd be a good idea. I'm not sure why the polyfill was authored the way it is, but we should make IE11 faster if this issue has been fixed.
...would be nice if someone from the core team could chime in.
@kevinpschaaf @sorvell @azakus @garlicnation - any chances that this issue will be tackled in v1 version?
@kevinpschaaf @sorvell @azakus @garlicnation as i see you guys are busy with webcomponents polyfill v1, does it make sense to fix this issue for the new version (https://github.com/webcomponents/html-imports) ?
If yes - I can help with testing and improving HTMLImport polyfill for IE.
cc-ing @valdrinkoshi as an FYI, as he's working on the v1 polyfill
What is the status of this issue? I am still having the same problem using the v1 poylfill
re wait for interactive: this is already done in the html-imports v1 polyfill