¿Cómo defiendo o asincrono este fragmento de javascript de WordPress para cargar por último para tiempos de carga de página más rápidos?


Tengo varios javascripts que son complementos necesarios en uno de mis dominios de WordPress, y sé desde dónde se llama en el archivo php.

Estoy tomando todas las medidas que puedo tomar para acelerar los tiempos de carga de la página, y cada probador de velocidad en la web dice que aplace los javascript si es posible.

He leído sobre las funciones defer='defer'y async en javascript y creo que una de ellas logrará lo que estoy tratando de lograr. Pero no lo soy. entendiendo cómo lo haría en un archivo php.

Por ejemplo, aquí hay un fragmento del archivo php de un complemento en particular donde se llama al archivo javascript:

function add_dcsnt_scripts() {

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js' );

}

He leído que es mejor hacer algo como esto para acelerar los tiempos de carga de la página:

<script defer async src="..."></script>

Pero no se como lograr eso dentro de un archivo php. Quiero hacer esto con todos mis archivos javascript.

¿Cómo lograría diferir o asincronar este fragmento de código javascript a is loads last y acelera los tiempos de carga de la página? ¿Cuál sería la forma ideal de aumentar los tiempos de carga de la página en todos los navegadores? ¡Gracias por cualquier orientación que alguien pueda ofrecer!

Author: vard, 2013-09-22

11 answers

Esta entrada de blog enlaza a dos plugins de interés:

Javascript asíncrono
Mejore el rendimiento de carga de páginas cargando javascript de forma asíncrona con head.js

WP Diferido Javascripts
Aplaza la carga de todos los javascript añadidos con wp_enqueue_scripts, usando LABJS (una biblioteca javascript asíncrona).

No los han probado, pero han comprobado el código y lo hacen cosas bastante elegantes con WordPress scripts proceso de eneuing.

Pero entonces WPSE viene a rescatar :

// Adapted from https://gist.github.com/toscho/1584783
add_filter( 'clean_url', function( $url )
{
    if ( FALSE === strpos( $url, '.js' ) )
    { // not our file
        return $url;
    }
    // Must be a ', not "!
    return "$url' defer='defer";
}, 11, 1 );
 26
Author: brasofilo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-13 12:37:31

O una forma más universal:

function add_async_forscript($url)
{
    if (strpos($url, '#asyncload')===false)
        return $url;
    else if (is_admin())
        return str_replace('#asyncload', '', $url);
    else
        return str_replace('#asyncload', '', $url)."' async='async"; 
}
add_filter('clean_url', 'add_async_forscript', 11, 1);

Para que pueda agregar async a cualquier script sin cambios de código, simplemente agregue #asyncload a la url del script como:

wp_enqueue_script('dcsnt', '/js/jquery.social.media.tabs.1.7.1.min.js#asyncload' )
 38
Author: bestlion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-19 02:21:31

Tratando de mantener las cosas algo modulares y abarcadoras, el siguiente enfoque elige dinámicamente cómo incrustar la etiqueta con los atributos async o defer simplemente agregando un pequeño identificador al nombre handle handle:

/**
* Add async or defer attributes to script enqueues
* @author Mike Kormendy
* @param  String  $tag     The original enqueued <script src="...> tag
* @param  String  $handle  The registered unique name of the script
* @return String  $tag     The modified <script async|defer src="...> tag
*/
// only on the front-end
if(!is_admin()) {
    function add_asyncdefer_attribute($tag, $handle) {
        // if the unique handle/name of the registered script has 'async' in it
        if (strpos($handle, 'async') !== false) {
            // return the tag with the async attribute
            return str_replace( '<script ', '<script async ', $tag );
        }
        // if the unique handle/name of the registered script has 'defer' in it
        else if (strpos($handle, 'defer') !== false) {
            // return the tag with the defer attribute
            return str_replace( '<script ', '<script defer ', $tag );
        }
        // otherwise skip
        else {
            return $tag;
        }
    }
    add_filter('script_loader_tag', 'add_asyncdefer_attribute', 10, 2);
}

Ejemplo de uso:

function enqueue_my_scripts() {

    // script to load asynchronously
    wp_register_script('firstscript-async', '//www.domain.com/somescript.js', '', 2, false);
    wp_enqueue_script('firstscript-async');

    // script to be deferred
    wp_register_script('secondscript-defer', '//www.domain.com/otherscript.js', '', 2, false);
    wp_enqueue_script('secondscript-defer');


    // standard script embed
    wp_register_script('thirdscript', '//www.domain.com/anotherscript.js', '', 2, false);
    wp_enqueue_script('thirdscript');
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts', 9999);

Salidas:

<script async type='text/javascript' src='//www.domain.com/somescript.js'></script>
<script defer type='text/javascript' src='//www.domain.com/otherscript.js'></script>
<script type='text/javascript' src='//www.domain.com/anothercript.js'></script>

Gracias a @MattKeys @crissoca por inspirar mi respuesta aquí.

 15
Author: Mike Kormendy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-11 18:13:40

Otra solución que usa un filtro diferente, que se puede usar para apuntar a un controlador de script específico:

function frontend_scripts()
{
    wp_enqueue_script( 'my-unique-script-handle', 'path/to/my/script.js' );
}
add_action( 'wp_enqueue_scripts', 'frontend_script' );

function make_script_async( $tag, $handle, $src )
{
    if ( 'my-unique-script-handle' != $handle ) {
        return $tag;
    }

    return str_replace( '<script', '<script async', $tag );
}
add_filter( 'script_loader_tag', 'make_script_async', 10, 3 );
 11
Author: Matt Keys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-05 01:41:19

Un método simplificado. Añadir a sus funciones.archivo php para hacer que JavaScript sea asíncrono en Wordpress

// Make JavaScript Asynchronous in Wordpress
add_filter( 'script_loader_tag', function ( $tag, $handle ) {    
    if( is_admin() ) {
        return $tag;
    }
    return str_replace( ' src', ' async src', $tag );
}, 10, 2 );
 9
Author: TheRealJAG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-01 19:59:39

Para obtener control sobre qué archivos js aplazar y evitar conflictos, puede agregar una variable a la url en la función wp_register_script como se muestra a continuación.

wp_register_script( 'menu', get_template_directory_uri() . '/js/script.js?defer', array('jquery'), '1.0', true );

Luego cambia la línea:

if ( FALSE === strpos( $url, '.js' ))

A:

if ( FALSE === strpos( $url, '.js?defer' ))

El nuevo filtro se ve así.

add_filter( 'clean_url', function( $url )
{
    if ( FALSE === strpos( $url, '.js?defer' ) )
    { // not our file
    return $url;
    }
    // Must be a ', not "!
    return "$url' defer='defer";
}, 11, 1 );
 4
Author: Nathan Friend,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-02 14:51:01

Incorporar el atributo async a los scripts para que solo se carguen una vez que se haya cargado toda la página

<script type="text/javascript">
    function ngfbJavascript( d, s, id, url ) {
        var js, ngfb_js = d.getElementsByTagName( s )[0];
        if ( d.getElementById( id ) ) return;
        js = d.createElement( s );
        js.id = id;
        js.async = true;
        js.src = url;
        ngfb_js.parentNode.insertBefore( js, ngfb_js );
    };
</script>

Fuente:Aquí

 1
Author: Anz Joy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-10 11:18:00

Algo para agregar la solución de filtro clean_url, asegúrese de validar para usarlo solo en el extremo de la fuente tal vez usando if( ! is_admin() ){} plugins populares como ACF podría darle un dolor de cabeza.

Update

Aquí está mi versión modificada de la solución:

if( ! is_admin() ){
  add_filter( 'clean_url', 'so_18944027_front_end_defer', 11, 1 );
  function so_18944027_front_end_defer( $url ) {
    if ( FALSE === strpos( $url, '.js' ) )
    { // not our file
        return $url;
    }
    // Must be a ', not "!
    return "$url' defer='defer";
  }
}
 1
Author: crissoca,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-28 04:37:12

Creo que es una mala práctica aplazar/async WordPress jQuery. Una mejor solución sería excluir jQuery del filtro:

if (!is_admin()) {
    add_filter( 'script_loader_tag', function ( $tag, $handle ) {    
        if ( strpos( $tag, "jquery.js" ) || strpos( $tag, "jquery-migrate.min.js") ) {
            return $tag;
        }
        return str_replace( ' src', ' async src', $tag );
    }, 10, 2 );
}

Puedes usar defer en lugar de async

 1
Author: Abukwaik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-08 12:49:52

Muy poco código de modificación Mike Kormendy , que le permite agregar 2 atributos a la vez:

// Async load
function add_asyncdefer_attribute($tag, $handle)
{
    $param = '';
    if ( strpos($handle, 'async') !== false ) $param = 'async ';
    if ( strpos($handle, 'defer') !== false ) $param .= 'defer ';
    if ( $param )
        return str_replace('<script ', '<script ' . $param, $tag);
    else
        return $tag;
}

Resultado:

<script async defer type='text/javascript' src='#URL'></script>
 1
Author: Marina Lebedeva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 10:37:23

La manera correcta de hacer esto

wp_script_add_data( 'script-handle', 'async/defer' , true );

Ver más

 0
Author: Sharif Mohammad Eunus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-02 08:47:55