-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
link element load event not fired on old browser. #294
Comments
@lili21 webpack everywhere uses Promises for loading async chunk, please use Promise polyfills, also your example about loading async |
@evilebottnawi I don't think you understand the issue. maybe it's my fault, my english isn't very good. It's not about Promise polyfills, and it's not the async and I think the issue is relate to this repo. as I mentioned before, the plugin using the example code logic is something like below, const p1 = loadAsyncJs()
const p2 = loadAsyncCss()
Promise.all([p1, p2]).then(() => {
// the function will never be called
console.log('App Loaded')
}) and |
@lili21 can you create minimum reproducible test repo and affected browsers? |
Yeah. here you go I only tested it on Android mobile. Android 4.4+ is good. Android 4.3 isn't. |
same error here. When use webpack dynamic |
You can extract all css in a single file to prevent this, But it's bad for performance. |
@lili21 I wouldn't say it's necessarily |
I present to you this horribleness as a workaround, which does work on Chrome 14, one of the affected browsers according to https://pie.gd/test/script-link-events/: I did not actually test this with // @ts-check
// Used for the test code
import 'core-js/fn/promise'
// Needed for the polyfill itself
// Chrome 14 didn't have this either
import MutationObserver from 'mutation-observer'
polyfill()
// Fun fact: can't import this over https, github's https is too new for Chrome 14
testImport('http://necolas.github.io/normalize.css/8.0.1/normalize.css').then(
function() {
console.log('Promise resolved!')
}
)
/**
*
* @param {string} url
*/
function testImport(url) {
// this code is the same as the one the webpack runtime will emit
return new Promise(function(resolve) {
var link = document.createElement('link')
link.rel = 'stylesheet'
link.type = 'text/css'
link.crossOrigin = 'anonymous'
link.onload = resolve
link.href = url
document.getElementsByTagName('head')[0].appendChild(link)
})
}
function polyfill() {
var webkitVersionMatch = navigator.userAgent.match(
/AppleWebKit\/(\d+(?:\.\d+)+)/
)
var webkitVersion = webkitVersionMatch && parseFloat(webkitVersionMatch[1])
// 535.24 is the first version listed as working
// in https://pie.gd/test/script-link-events/
if (webkitVersion === null || webkitVersion >= 535.24) {
return
}
var copyCrossOrigin = document.createElement('link').crossOrigin !== undefined
var mo = new MutationObserver(function(events) {
events.forEach(function(event) {
event.addedNodes.forEach(function(node) {
// prettier-ignore
if (/** @type {Element} */ (node).tagName === 'LINK') {
handleLinkTag(/** @type {HTMLLinkElement} */ (node))
}
})
})
})
mo.observe(document.getElementsByTagName('head')[0], {
childList: true
})
/**
* @param {HTMLLinkElement} tag
*/
function handleLinkTag(tag) {
if (
tag.rel !== 'stylesheet' ||
tag.type !== 'text/css' ||
tag.onload === null
) {
// don't care
return
}
setTimeout(dispatch)
function dispatch() {
var img = new Image()
// webpack doesn't care about the resolved value, only that it resolved
img.onerror = /** @type {() => void} */ (tag.onload)
img.onload = tag.onload
// NOTE: only copy crossOrigin if the browser supports crossOrigin to begin with
if (copyCrossOrigin) {
img.crossOrigin = tag.crossOrigin
}
img.src = tag.href
}
}
} This does, of course, make it impossible to detect real errors when loading the script; the Promise will always resolve. You can replace the contents of the |
MCEP
useload
event to resolve the promise, while the event is not supported well on some old browser, like Android 4.3 webview.It breaks the webpack
dynamic import
feature, cause the promise will never be resolved.Reproduce repo
The text was updated successfully, but these errors were encountered: