Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to prevent unnecessary AJAX? #9365

Closed
DavidAnderson684 opened this issue Oct 15, 2015 · 26 comments
Closed

A way to prevent unnecessary AJAX? #9365

DavidAnderson684 opened this issue Oct 15, 2015 · 26 comments

Comments

@DavidAnderson684
Copy link
Contributor

@DavidAnderson684 DavidAnderson684 commented Oct 15, 2015

I'm currently looking at the performance of multiple WooCommerce-powered websites' home pages.

On each of them, the home page is (or could be) entirely being served from CDNs and caches, except for one PHP call invocation, for WC's ajax call (?wc-ajax=get_refreshed_fragments).

Since the home pages have nothing WooCommerce-related on them - no cart, no products, nothing - this seems like it something that could be optimised away.

I can't see any way to accomplish this. As far as I can see, WC neither attempts to optimise this call away itself, nor provides a way for a site builder to manually force it. As a result, every page on the site, regardless of whether the page has any WC-related content, generates a call to PHP on the back-end. This looks like it may be the largest contributor to server load for many pages, given that, as I say, they're otherwise being delivered from cache/CDN.

Is there any way in which this aspect of site performance can be improved, even if it has to come down to "a site builder has a way to manually force some pages to skip this call" ?

@mikejolley
Copy link
Member

@mikejolley mikejolley commented Oct 15, 2015

De-enqueue the script. WC has no way to know if your page does or does not have a cart widget on it.

@mikejolley mikejolley closed this Oct 15, 2015
@DavidAnderson684
Copy link
Contributor Author

@DavidAnderson684 DavidAnderson684 commented Oct 15, 2015

For anyone who finds this, here's an example:

add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); function dequeue_woocommerce_cart_fragments() { if (is_front_page()) wp_dequeue_script('wc-cart-fragments'); }

Though, personally In think WC could be improved by defining a constant if any of the in-built cart widgets are painted... and then a developer could use that constant to de-queue if he finds that constant not set. (Of course, this won't catch the case of third-party widgets/page elements that rely on it - but they could also start using the constant. And every little helps).

@vanhallman
Copy link

@vanhallman vanhallman commented Jan 2, 2017

Hey #DavidAnderson684, Happy New year! Newbie question: Where do I place "add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); function dequeue_woocommerce_cart_fragments() { if (is_front_page()) wp_dequeue_script('wc-cart-fragments'); }"

@creart40
Copy link

@creart40 creart40 commented Jan 7, 2017

Hi @vanhallman you need to add in your functions.php theme. I have tested it and it founds! ;)

@cranky69
Copy link

@cranky69 cranky69 commented Mar 6, 2017

Thank you. but where exactly i need to put this code ? at last line before } ?

@lukecav
Copy link

@lukecav lukecav commented Mar 31, 2017

@DavidAnderson684

That example works great.

@artbox
Copy link

@artbox artbox commented Apr 14, 2017

The placed code is working fine for homepage, but I still can find "?wc-ajax=get_refreshed_fragments" on Posts and some other pages. Did you check them too or there is no problem on your site(s) ?

@Shypixel
Copy link

@Shypixel Shypixel commented Apr 18, 2017

@artbox

Try this - I just put it in place and we're humming along fine with 139k uniques a day.

add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11);
function dequeue_woocommerce_cart_fragments() {
if (is_front_page() || is_single() ) wp_dequeue_script('wc-cart-fragments');
}

Note: pretty sure this willmuck with things if you have cart notifications and such in your theme, or if you try to embed products or other WC widgets on post pages. But if you know you don't have cart elements on post pages, it'll work.

@jessuppi
Copy link

@jessuppi jessuppi commented Oct 18, 2017

If anyone is interested, we made a free WordPress plugin based on this GitHub issue:

https://wordpress.org/plugins/disable-cart-fragments-littlebizzy/

@blue928
Copy link

@blue928 blue928 commented May 21, 2018

We're hitting this issue too with added load times between 1 and 2 seconds. Dequeuing fixes the issue, but what effect will this have on my store? Is this script needed for core functionality? What are the caveats of it not being enqueued? Can something break?

@lukecav
Copy link

@lukecav lukecav commented Aug 16, 2018

@blue928

Do you have mini cart in the header of that store, if not then you will be fine.

@blue928
Copy link

@blue928 blue928 commented Aug 19, 2018

@lukecav Do you mean the little cart icon that can be added to the navigation? We do have that as a part of our menu. It will show a numeric value depending on the number of items in the cart.

https://toursoft.co

If that's what you mean, we're happy to get rid of that to save 1 to 2 seconds of load time.

@lukecav
Copy link

@lukecav lukecav commented Aug 21, 2018

@blue928

Yep correct cart icon in the header, replace it for a menu link in the nav to the cart page on the site.

@makeonlineshop
Copy link

@makeonlineshop makeonlineshop commented Nov 16, 2018

hello, as I do not have any ajax cart on my shop, can I disable cart fragment for whole website with this code ?
define('DISABLE_CART_FRAGMENTS');

Thank you for your help.

@jessuppi
Copy link

@jessuppi jessuppi commented Nov 16, 2018

@makeonlineshop

Nope, there is no built-in functionality to disable cart fragments, you must use the plugin.

If the plugin is active, no defined constant or otherwise is needed.

@makeonlineshop
Copy link

@makeonlineshop makeonlineshop commented Nov 16, 2018

@makeonlineshop

Nope, there is no built-in functionality to disable cart fragments, you must use the plugin.

If the plugin is active, no defined constant or otherwise is needed.

hello,

I finally found this, so it seems that the plugin is not needed ? Thank you.

/**

  • xxxxxx FUNCTION TO DISABLE cart fragment on all pages
    */
    add_action( 'wp_print_scripts', 'nuke_cart_fragments', 100 );
    function nuke_cart_fragments() {
    wp_dequeue_script( 'wc-cart-fragments' );
    return true;
    }
@pieterjanliekens
Copy link

@pieterjanliekens pieterjanliekens commented Jan 4, 2019

This is not a good solution...

@pieterjanliekens
Copy link

@pieterjanliekens pieterjanliekens commented Jan 4, 2019

We're hitting this issue too with added load times between 1 and 2 seconds. Dequeuing fixes the issue, but what effect will this have on my store? Is this script needed for core functionality? What are the caveats of it not being enqueued? Can something break?

Did you ever get to fix this?

@HellmutW
Copy link

@HellmutW HellmutW commented Jan 19, 2019

We're hitting this issue too with added load times between 1 and 2 seconds. Dequeuing fixes the issue, but what effect will this have on my store? Is this script needed for core functionality? What are the caveats of it not being enqueued? Can something break?

Did you ever get to fix this?

This seems to work:
/** Disable Ajax Call from WooCommerce on entire website*/ add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); function dequeue_woocommerce_cart_fragments() { wp_dequeue_script( 'wc-cart-fragments' ); return true; }

I simply replaced the woocommerce button in my header with a button which is linking straight to my cart page. No ajax needed anymore. The user has to change pages to see the cart, but it saves ~1s on each page.

@makeonlineshop
Copy link

@makeonlineshop makeonlineshop commented Jan 19, 2019

We're hitting this issue too with added load times between 1 and 2 seconds. Dequeuing fixes the issue, but what effect will this have on my store? Is this script needed for core functionality? What are the caveats of it not being enqueued? Can something break?

Did you ever get to fix this?

This seems to work:
/** Disable Ajax Call from WooCommerce on entire website*/ add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11); function dequeue_woocommerce_cart_fragments() { wp_dequeue_script( 'wc-cart-fragments' ); return true; }

I simply replaced the woocommerce button in my header with a button which is linking straight to my cart page. No ajax needed anymore. The user has to change pages to see the cart, but it saves ~1s on each page.

hello, is it better than my code above ? thanks.

@HellmutW
Copy link

@HellmutW HellmutW commented Jan 23, 2019

To be honest, I did not try your code @makeonlineshop, since @pieterjanliekens said it might not be good. I am not entirely sure why though. However, nuking a function and simply disabling it probably makes a difference.
So I made my code, partly also because the Plugin from @jessuppi is not downloadable anymore since 13 Dec 2018.

@jessuppi
Copy link

@jessuppi jessuppi commented Jan 23, 2019

It's always been available on GitHub:

https://github.com/littlebizzy/disable-cart-fragments

@HellmutW
Copy link

@HellmutW HellmutW commented Jan 23, 2019

It's always been available on GitHub:

https://github.com/littlebizzy/disable-cart-fragments

That is great @jessuppi, I might try this instead. I just saw that it is unavailable for download on wordpress.org since last month. Thank you very much.

@pieterjanliekens
Copy link

@pieterjanliekens pieterjanliekens commented Jan 26, 2019

wc-cart-fragments are the items in the cart,
If you have an ajax cart/sidebar with cart contents, de-enqueing this will make an initial page load from an existing user have an empty cart...

For now I've deenqued it on the homepage, so initial page loads take 2-3 seconds less long.
Yes... on a shared hosting this does slow down the page load tremendously.

@chriseckman
Copy link

@chriseckman chriseckman commented Feb 11, 2019

Dequeuing will work however you have to do it after anything else might have enqueued it. This will fix it. https://www.eckmandesign.com/blog/is-woocommerce-slowing-down-your-site-lets-fix-it/

@KVNHNF
Copy link

@KVNHNF KVNHNF commented Dec 25, 2019

added that snippet however /wp-content/plugins/woocommerce/assets/js/frontend/cart-fragments.min.js?ver=3.8.1 is still loading on GTMETRIX waterfall...

Is it because it is the shop page? and is_archive() should be added too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.