Google PageSpeed, a good tool to test and get advices from
Open the PageSpeed page: https://developers.google.com/speed/pagespeed/insights/ and enter the website URL you want to optimizeYou will get a score and some suggestions. There should be some 'should fix' ones and some 'consider fixing' ones, and we should do our best to fix the ones marked as 'should fix'
Use drupal async_js module if there are numerous render-blocking javascript files
Install the module from the website : https://www.drupal.org/project/async_js and enable the moduleOne way to add the async for loading the js is using the built-in function in this module as below:
async_js_add_js("path/to/your/script.js")
The other way is to add an attribute in the options of drupal_add_js function as the following code:
drupal_add_js("path/to/your/script.js", array("async_js" => TRUE))
Note: some of the js might have dependencies, so this module also support setting the dependencies of the js by adding the attribute of async_dependencies.
Always try to add the js via drupal_add_js function instead of in the template header
The reason is that when the js is added via the function, you can control where to load the js(in the header or footer) and the weight in the loading process. Here are some examples of doing it:
//load js in the footer
drupal_add_js('sites/all/themes/ora_theme/js/jquery-1.9.1.js',
array('type' => 'file', 'scope' => 'footer', 'weight' => 22));
//load js in the header
drupal_add_js('sites/all/themes/ora_theme/js/typekit.js',
array('type' => 'file', 'scope' => 'header', 'weight' => 21));
No comments:
Post a Comment