4

I am trying to load some of my scripts from CDNs like CDNjs and Google, The scripts are being loaded correctly, but for some reason I do not find, for each script I have two or even three HTTP request (For the same script), here is an example: http://tools.pingdom.com/fpt/#!/ePuR3Z/http://elbauldelprogramador.com.

I have notice that when I am logged in, all scripts are generating only one HTTP request.

Jquery from ajax.googleapis is generating two http request, and jquery.easing.min.js from cdnjs three.

The code is :

//Making jQuery Google API
function modify_jquery() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js', false, '2.0.3');
        wp_enqueue_script('jquery');
   }
}
add_action('init','modify_jquery');

I have a child theme and from his parent I have copied a function called wi_enqueue(). In the parent the function is like this:

add_action( 'wp_enqueue_scripts', 'wi_enqueue' );
if ( !function_exists('wi_enqueue') ) {
function wi_enqueue(){
 /* ...*/
}

In my child function.php I have:

function wi_enqueue(){
    global $wp_styles, $smof_data;

    /* Enqueue */
    wp_enqueue_script( 'wi-easing', '//cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js', array('jquery'), '1.3', true );  
    wp_enqueue_script( 'wi-touchswipe', '//cdnjs.cloudflare.com/ajax/libs/jquery.touchswipe/1.6.4/jquery.touchSwipe.min.js', array('jquery'), '1.3.3', true );
    wp_enqueue_script( 'wi-autosize', '//cdnjs.cloudflare.com/ajax/libs/autosize.js/1.17.1/autosize-min.js', array('jquery'), '1.17.1', true );
    wp_enqueue_script( 'wi-placeholder', '//cdnjs.cloudflare.com/ajax/libs/placeholders/2.1.0/placeholders.min.js', array('jquery'), '2.1.0', true );
    wp_enqueue_script( 'wi-modernizr', get_template_directory_uri() . '/js/modernizr.custom.15463.js', array('jquery'), '2.6.2', true );
    wp_enqueue_script( 'wi-waypoint', '//cdnjs.cloudflare.com/ajax/libs/waypoints/2.0.2/waypoints.min.js', array('jquery'), '2.0.2', true );
    wp_enqueue_script( 'wi-tipsy', get_template_directory_uri() . '/js/jquery.tipsy.js', array('jquery'), '1.0.0', true );
    wp_enqueue_script( 'wi-fitvids', get_template_directory_uri() . '/js/jquery.fitvids.js', array('jquery'), '1.0', true );
    wp_enqueue_script( 'wi-sidr', get_template_directory_uri() . '/js/jquery.sidr.min.js', array('jquery'), '1.1.1', true );

/* .... */
}
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
  • 6
    You should really really not do so. it is a very [bad practice](http://pippinsplugins.com/why-loading-your-own-jquery-is-irresponsible/). Please just drop this method. There is nothing wrong with wordpress own jQuery ( except for very extreme case-use. ) – Obmerk Kronen Jan 06 '14 at 01:42
  • I thought it was right. Thank you for pointing it out – Alejandro Alcalde Jan 07 '14 at 12:02
  • @ObmerkKronen Please stop spreading around misinformation like this. It's perfectly acceptable to replace Wordpress' own jQuery with the same version from a CDN as long as jQuery.noconflict() is still called – Andy Feb 26 '14 at 11:03
  • 1
    @Andy, this is not misinformation. CDN´s do not work the same in many countries as they do in the U.S. or in eastern Europe. for example, in china ( third of the world´s internet users ) google CDN´s work sporadically, if at all. When do work, loading times of sites can increase by 200%.. I suggest you would do your homework on the subject before accusing others of `"spreading around misinformation"`. If it was such a good solution, it would have been in core long ago. Not to mention the fact that in this question The jQuery loaded from Google is NOT in no conflict mode, – Obmerk Kronen Feb 26 '14 at 18:39
  • 1
    @Andy [some](http://wordpress.stackexchange.com/questions/108992/as-a-plugin-developer-how-to-deal-with-theme-developer-loading-jquery-from-goog) [reading](https://core.trac.wordpress.org/ticket/22896) [material](https://core.trac.wordpress.org/ticket/23658#comment:3). That's 3 links for easy educational reading. And if you do not believe those core developers, I think I can also find you the comments of Matt Mullenweg on the subject. – Obmerk Kronen Feb 26 '14 at 18:53
  • @ObmerkKronen I've been using the Google CDN for around 3 years and never had an issue with downtime. Are you trying to suggest loading jQuery from a Google CDN is actually slower than from the website's own server? The only reason against it is so theme developers don't have to update the jQuery version requested from the CDN when a Worpdress update ships with a newer one. noConflict() can (and should) be set by the theme developer after the CDN jQuery is loaded. Thanks for the links, always good to read more opinion on it. Link two is regarding wp-admin though so is not relevant here – Andy Feb 26 '14 at 19:37
  • 1
    @Andy - DO what you will. the point is - If you had a problem or not you CAN NOT KNOW . you probably are developing small local websites and you never really tried to check them from abroad . Like said in comment link above - you have no idea if your user have or not access to the CDN. You have no idea of their load times . you have no idea of the users lost. The fact that `YOU` had no problems does not make it a good practice and does not make me `spreading around misinformation`. – Obmerk Kronen Feb 27 '14 at 01:29
  • @Andy ..and yes, not only I am suggesting it is [slower](http://tomasdostal.com/jquery-cdn-vs-google-cdn-where-to-load-scripts-from/).I am saying sometimes it will [NOT](http://stackoverflow.com/questions/2607425/is-googles-cdn-for-jquery-available-in-china) [WORK](http://stackoverflow.com/questions/2607425/is-googles-cdn-for-jquery-available-in-china) [at](http://wordpress.org/support/topic/china-users-failed-in-fontsgoogleapiscom) [all](https://make.wordpress.org/core/2013/11/11/open-sans-bundling-vs-linking/) (4).But you might want to post a question and get a proper answer before accusing. – Obmerk Kronen Feb 27 '14 at 01:49
  • @Andy Not to mention that the OP is including a different version of jQuery . Talking about misinformation. And by the way - in all your alleged accusations and stories of own limited experience you have failed to mention the `PROs` of using the Google CDN ( except the fact that probably you have read it in some dubious amateurial web tutorials ) Like @Obmerk kronen gracefully stated before accused by you of spreading misinformation - this practice is only excused in very specific and rare case scenarios. – krembo99 Feb 27 '14 at 02:09
  • [FALLBACK](http://stackoverflow.com/a/5531821/1244126) is the only valid way of using ANY CDN . and since it has virtually no benefits - this whole discussion is a bit absurd . If @Andy wants to use CDNs - No problems here . But preaching for it without proper knowledge or education on the matter where it can later cause other users serious troubles is a bit wrong IMHO. Final Note I hope. – Obmerk Kronen Feb 27 '14 at 08:10
  • @ObmerkKronen You're making assumptions on the size of sites I build and experience I have to validate your point. Well done! The fact of the matter is, you've read a few links saying why it's bad and jumped on the bandwagon – Andy Feb 27 '14 at 09:21
  • [I](http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/) [can](http://stackoverflow.com/questions/2180391/why-should-i-use-googles-cdn-for-jquery) [post](http://www.jquerybyexample.net/2012/01/why-to-use-google-hosted-jquery-cdn.html) [links](http://www.wpbeginner.com/wp-themes/replace-default-wordpress-jquery-script-with-google-library/) [too](http://www.unseenrevolution.com/jquery-library-from-cdn-in-wordpress/) :) – Andy Feb 27 '14 at 09:32
  • @Andy. I did not "read a few link" .Through my work I visit china frequently in the past 5 years as well as other 25 countries. I speak from personal experience. And I am not the one making accusations or assumptions. If you would have checked your site from an oversea proxy, you would not have made the "misinformation" comment / accusation. But this is not a discussion board . we can get this [to chat](http://chat.stackoverflow.com/rooms/info/48553/to-cdn-or-not-to-cdn?tab=general) . – Obmerk Kronen Feb 27 '14 at 09:35
  • 1
    @Andy, One last comment if you refuse to chat - You should READ the links you posted .. If you read carefully - they actually help my point quit a bit . I would also like to know what `"misinformation"` I should `"stop spreading around like this"` ( and like what.. ?? ). – Obmerk Kronen Feb 27 '14 at 09:47

2 Answers2

1

This may or may not resolve your question, but I had a very similar issue a while ago and spent a long time pulling my hair out looking for a solution. In the end it turned out that some browsers (Firefox being the most common of the guilty ones) prefetch the next post/article, including JS and other page elements, based on the <link rel="next" /> tag.

There's a lot more information on this here: http://www.ebrueggeman.com/blog/wordpress-relnext-and-firefox-prefetching

Hopefully this will help, if you're still having the issue.

Mark Ormesher
  • 2,289
  • 3
  • 27
  • 35
0

If scripts are being loaded more than once, review your source code and save it once again. Then you might want to clear your browser cache to make sure that the previous cache is cleared so you can view the current updated website. If you still have the same problem it might be because of your template or wordpress not properly installed/configured.

Thus, I do highly recommend you to post a thread on wordpress.org instead if you still face the same problem. Hope this helps.

Yurii
  • 4,811
  • 7
  • 32
  • 41