can breaks any dynamicaly injected external script
-
I use Autoptimize, Async Javascript, Gutenberg and ‘Ghost Kit’ – it’s an extension to Gutenberg + extra components. Ghost Kit uses fetch API which makes Gutenberg load JS fetch API polyfill inline.
This is correct way: (Javascript Async disabled)
( 'fetch' in window ) || document.write( '<script src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-fetch.min.js"></scr' + 'ipt>' );( document.contains ) || document.write( '<script src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-node-contains.min.js"></scr' + 'ipt>' );( window.FormData && window.FormData.prototype.keys ) || document.write( '<script src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-formdata.min.js"></scr' + 'ipt>' );( Element.prototype.matches && Element.prototype.closest ) || document.write( '<script src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-element-closest.min.js"></scr' + 'ipt>' );
and this is what happens when I activate Async Javascript:
( 'fetch' in window ) || document.write( '<script async='async' src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-fetch.min.js"></scr' + 'ipt>' );( document.contains ) || document.write( '<script async='async' src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-node-contains.min.js"></scr' + 'ipt>' );( window.FormData && window.FormData.prototype.keys ) || document.write( '<script async='async' src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-formdata.min.js"></scr' + 'ipt>' );( Element.prototype.matches && Element.prototype.closest ) || document.write( '<script async='async' src="https://woo.localhost.test/wp-includes/js/dist/vendor/wp-polyfill-element-closest.min.js"></scr' + 'ipt>' );
As you can see JS Async, adds async=’async’ attribute to document.write string breaking JS snippet. This cause JS error “ncaught SyntaxError: missing ) after argument list” as described here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_parenthesis_after_argument_list.
The solution would be to wrap async/defer value using double quotation rather than single. This bug, will break any script injected this way.
Can you tell if what is the simplest way to sort that issue? Thanks!
- The topic ‘can breaks any dynamicaly injected external script’ is closed to new replies.