Open bschlenk opened 4 years ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
lit Project Browser gives error to not use document.write
changing this line would be nice:
var newScript = document.createElement('script');
newScript.src = url;
// if readyState is 'loading', this script is synchronous
if (document.readyState === 'loading') {
// make sure custom elements are batched whenever parser gets to the injected script
newScript.setAttribute(
'onload',
'window.WebComponents._batchCustomElements()'
);
document.write(newScript.outerHTML);
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Description
If
webcomponents-loader.js
is loaded asynchronously, but runs whiledocument.readyState
is stillloading
, then it fails to load the polyfills.Steps to Reproduce
webcomponents-loader.js
script asynchronously on a pagedocument.readyState === 'loading'
when the script is run by doing a lot of work in<head>
Promise
, the script then fails at https://github.com/webcomponents/polyfills/blob/master/packages/webcomponentsjs/webcomponents-loader.js#L84Expected Results
The loader works when loaded asynchronously while
document.readyState
is stillloading
.Actual Results
Error thrown because
Promise
is not defined.Browsers Affected
Prior Work
I'm basically reopening https://github.com/webcomponents/polyfills/issues/57. There was a pull request in the pre-monorepo world that tried solving this: https://github.com/webcomponents/webcomponentsjs/pull/1036
It never got merged because tests for ie11 & edge were failing. The issue with that PR is that it relies on
document.currentScript
to check if the script that initiated the polyfills isasync
, which is not supported in ie11.Discussion
I've come up with a different approach that I'd like to discuss & potentially open up a PR for. The patch is pasted below. I'd like to understand why the loader currently uses
document.write
, and what sort of issues switching away from that approach would cause. The changes that I'm using make it so that polyfills andDOMContentLoaded
are always waited on, and the polyfills script tag is always added viadocument.head.appendChild
.