[wp-trac] [WordPress Trac] #22470: WP_Scripts: output script element only if src not empty

WordPress Trac noreply at wordpress.org
Fri Nov 16 00:36:50 UTC 2012


#22470: WP_Scripts: output script element only if src not empty
-----------------------------+-------------------------
 Reporter:  niallkennedy     |       Type:  enhancement
   Status:  new              |   Priority:  normal
Milestone:  Awaiting Review  |  Component:  General
  Version:                   |   Severity:  trivial
 Keywords:  has-patch        |
-----------------------------+-------------------------
 I was able to make async JavaScript loading work with existing !WordPress
 code by acting on the script_loader_src filter before the `<script>`
 element is output, returning an empty string. This results in an empty src
 attribute, creating technically invalid HTML while not immediately loading
 the external script. If the !WordPress code could check if a non-empty
 `$src` string exists before outputting I could avoid the invalid HTML nag.
 Attached diff only outputs the `<script src="">` element if `$src` is not
 an empty string.

 I'd like to be able to asynchronously load an enqueued script without
 needing to completely rework `WP_Scripts`. I'd like to take advantage of
 common handles indicating a script is set to be loaded, extra data
 attached to a handle, and other !WordPress script expectations while
 swapping out the `<script src="">` echoed markup.

 Why async? A script steps out of the way, offering progressive enhancement
 of the page or loading non-critical features such as stats or socal
 network sharing buttons. Async loading also helps prevent single points of
 failure in webpage loading caused by China blocking Facebook or resources
 hosted at Amazon Web Services going offline for a few hours.

 * [http://www.stevesouders.com/blog/2012/01/13/javascript-performance/
 Some info from Steve Souders] showing a 31% increase in load times.
 * [https://developers.google.com/analytics/devguides/collection/gajs/
 Google Analytics use case]
 * [https://developers.facebook.com/docs/reference/javascript/#loading
 Facebook JavaScript SDK use case]

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22470>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list