Script loader updates
There are several updates to the script loader currently in WordPress 2.8-bleeding-edge that enhance and optimize loading of external JavaScript and CSS files.
Probably the most important change is that scripts can be queued for loading in the footer for both the admin and the front-end. This is done with an optional argument. To enqueue a script for the footer:
wp_enqueue_script( 'name', 'url/to/file.js', array('dependency'), 'version', true );
where “true” means enqueue for the footer (“false” is the default and is optional).
When a script is enqueued for the footer all dependencies will be added (if not already present) and will be printed before the script. Some may be in the head, others also in the footer. By default only jQuery is printed in the head but when a script is enqueued for the head, all dependencies would also be printed in the head. Almost all external scripts would run onload or after the page has loaded, so there’s no real need to queue anything for the head.
Scripts queued for the front-end footer depend on wp_footer(); being present in the current theme. Unfortunately some themes don’t include it. The best way to remedy this would be to bring awareness among users and theme designers as suggested by several plugin developers.
To make queueing of scripts easier two new actions have been added: "wp_enqueue_scripts" that runs in the front-end head where all is_page(), is_home(), etc. functions are available and "admin_enqueue_scripts" that runs in the admin head and has the current page hook as argument, so scripts can be queued only for specific pages.
Another major new feature is that all core admin scripts are concatenated and compressed before sending them to the browser. This feature can easily be extended to include scripts added by plugins and to use server side caching, however that would require some changes to the server settings (.htaccess on Apache).
Since compression from php can be problematic on some hosts there are several “switches” (constants) that manage it: define('CONCATENATE_SCRIPTS', false); would turn off both concatenating and compressing of all scripts. It’s intended for script debugging, define('COMPRESS_SCRIPTS', false); can be used to turn off compression for JavaScript and define('COMPRESS_CSS', false); for CSS files. Compression is set to “deflate” by default since it’s faster and uses a little less server resources. Gzip can be forced by setting define('ENFORCE_GZIP', true);
There is a test if compressing from php works as expected on the server and whether the server compresses scripts by default. It runs only once and saves the result in an option “can_compress_scripts”. It would run again if the option is deleted.
In addition all core scripts are minified. All custom scripts are included in two versions: .dev.js is the non-minified script and .js is the minified one. The constant define('SCRIPT_DEBUG', true); would load the .dev.js versions of the scripts making them easier to debug.
Possible changes: removing the COMPRESS_CSS switch and using only COMPRESS_SCRIPTS, using deflate for compression but adding the gzip file header and serving it as “Content-Encoding gzip” since it seems more compatible with the various web servers and proxyes (all modern browsers support deflate well).
GaMerZ 7:38 pm on February 6, 2009 Permalink
Hi Andrew,
I also blogged about this 2 weeks ago, http://lesterchan.net/wordpress/2009/01/26/loading-javascript-in-footer-in-wordpress-28/ =D
GaMerZ 7:44 pm on February 6, 2009 Permalink
I was hoping for concatenating API so that plugin author can use it. But I saw the path of script-loader.php, it seems to be in the /wp-admin/ folder. If it is extended to plugins, I think it would be good to move to /wp-includes/
Hailin 12:24 am on February 10, 2009 Permalink
In wp-includes/script-loader.php, the
line $scripts->add( ‘jquery’, ‘/wp-includes/js/jquery/jquery.js’, false, ’1.2.6-1′)
accounts for about 2 seconds of total page load time on any simple blog such as mary.wordpress.com. With it, the load time is: 4.94s; without it 2.88
Specifically,
http://s2.wordpress.com/wp-includes/js/jquery/jquery.js?ver=1.2.6-1 153ms
http://mary.wordpress.com/wp-content/plugins/mshots/jquery-ui-personalized-1.6rc2.packed.js?ver=MU 335ms
http://mary.wordpress.com/wp-content/plugins/mshots/jquery.cluetip.min.js?ver=MU 204ms
http://mary.wordpress.com/wp-content/plugins/mshots/mshots.js?ver=4 434 ms
A principle of frontend is to load js at the end of page, or as further down as possible. Not sure how further down we can move it, just throw in some thoughts here.
ronald 12:07 pm on February 28, 2009 Permalink
At the moment I am having issues with the jquery being loaded in the scriptloader. I include the 1.2.6 version via Google (http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js), and a dependent script will only work (frontend) if I take out the jquery from the scriptloader. This however will make the write post in the dashoard useless.
So I would like to be able to conditionally include jquery in scriptloader for admin and/or frontend.
Does this make sense?
WordPress 2.8 new feature preview | Ramoonus.nl 9:33 am on May 3, 2009 Permalink
[...] Note: see Lester Chan’s Loading Javascript in Footer blog and Andrew Ozz’s Script Loader Updates blog [...]
bingorabbit 4:29 am on June 11, 2009 Permalink
I had a problem with my theme after updating to 2.8, adding true solved it, thanks a lot
jfoucher 7:59 am on June 16, 2009 Permalink
Good post, but specifically, how would I go about compressing and concatenating scripts on the front end ?
Thanks
Le novità di WordPress 2.8 | Innovidea.net 10:38 am on July 12, 2009 Permalink
[...] Nota: si veda Lester Chan – Loading Javascript in Footer blog ed Andrew Ozz – Script Loader Updates [...]
WordPress 2.8.1 Released 1:49 am on July 15, 2009 Permalink
[...] Note: see Lester Chan’s Loading Javascript in Footer blog and Andrew Ozz’s Script Loader Updates blog [...]