I want to manually eliminate render blocking in order to boost speed.
I've been searching for solution in google for past two days.
Tried below 3 solutions in functions.php. That indeed worked however....
- eliminated from half js scripts and i still get the same message for other js.
- did not eliminate from CSS
Solution 1
add_filter( 'clean_url', function( $url )
{
if ( FALSE === strpos( $url, '.js' ) )
{
return $url;
}
return "$url' defer='defer";
}, 11, 1 );
Solutions 2 given here
add_action('customize_register', 'customizer');
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url' defer ";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
Solutions 3
// add async and defer to javascripts
function wcs_defer_javascripts ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url' async='async";
}
add_filter( 'clean_url', 'wcs_defer_javascripts', 11, 1 );
Please do not suggest any plugin. I have tried w3 cache, autoptimize and many more.
W3 cache did the job but that is not permanent solution. Because site deforms once w3 cache is deactivated (site does not return to normal even after reactivating).
Solution 4 added this code before
<script type="text/javascript">
function downloadJSAtOnload() {
var links = ["wp-content/themes/TL/library/js/scriptsslider.js", "wp-content/themes/TL/library/js/scriptsslider.js"],
headElement = document.getElementsByTagName("head")[0],
linkElement, i;
for (i = 0; i < links.length; i++) {
linkElement = document.createElement("script");
linkElement.src = links[i];
headElement.appendChild(linkElement);
}
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>