failed-to-load
v1.0.0
Published
Find script tag that have failed to load when document's readyState is complete
Downloads
11,759
Readme
failed-to-load
Find script tags that have failed to load when document's readyState is complete.
Add this attribute "onload" with "this.loaded=1" in its value (onload="this.loaded=1"
)
<script src="my-script-922a511f02d148e4c9390526d85ca519.js" onload="this.loaded=1"></script>
<script src="other-script-693e810ff27604e6da274da4c77e136c.js" onload="this.loaded=1"></script>
Register check for window load
import { register } from 'failed-to-load';
register();
This will throw an ayncronous error to be caught by your global onerror callback.
Error{ message: `Failed to load necessary script tags upon window load my-script-922a511f02d148e4c9390526d85ca519.js other-script-693e810ff27604e6da274da4c77e136c.js`, code: 'ERROR_NECESSARY_SCRIPT_NOT_LOADED', files: [ [object HTMLScriptElement], [object HTMLScriptElement] ] }
Alternatively, you can register a callback to catch the error (then it will not be thrown)
register(
error => logger.warn(error)
);
Check and report manually:
import { check } from 'failed-to-load';
window.addEventListener('load', () => {
const missing = check();
if (missing.length) {
logger.warn({
message: 'script files not loaded on page load',
files: missing.map(({ src }) => src)
})
}
});