Is there a way to log asset load errors on non-async assets when using a CSP with nonces for scripts?

Viewed 86

Because enabling use of nonces in CSP disables event handlers, it's not possible to add an onerror property to script tags to handle/report asset load issues.

Is there a recommended way to handle this? Particularly for script tags which are not loaded async.

Thanks!

2 Answers

If you dynamically load an external script, you can hang CSP-safe event listeners:

let script = document.createElement('script');

script.src = "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"
document.head.append(script);

script.addEventListener('error', function(e) {
    // handle onerror event here
    });

try creating a separate service that only purpose to monitor, handle, and report/display errors.

Related