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 · 30 comments
Closed

A way to prevent unnecessary AJAX? #9365

DavidAnderson684 opened this issue Oct 15, 2015 · 30 comments

Comments

@DavidAnderson684
Copy link
Contributor

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

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

@DavidAnderson684
Copy link
Contributor Author

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

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 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 commented Mar 6, 2017

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

@lukecav
Copy link
Contributor

lukecav commented Mar 31, 2017

@DavidAnderson684

That example works great.

@artbox
Copy link

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

@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

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 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
Contributor

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 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
Contributor

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

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

@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

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

This is not a good solution...

@pieterjanliekens
Copy link

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

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

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

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

It's always been available on GitHub:

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

@HellmutW
Copy link

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

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

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 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?

@suneth2
Copy link

suneth2 commented Feb 28, 2021

we can remove that completely but it will not show mini cart items when page load or refresh, so any one how to call this function after window load, so it will not effect for speed scores on major sites like gemetrix

@fradaspa
Copy link

fradaspa commented Apr 6, 2021

Hello guys. somebody can help me out?

?wc-ajax=get_refreshed_fragments | (canceled)

I got this call from all the pages even if that i don t have a cart in the site, and it let me lose 5 sec. What can I do?

@lukecav
Copy link
Contributor

lukecav commented Apr 6, 2021

@fradaspa

You can disable that cart fragment request using this plugin.
https://wordpress.org/plugins/disable-cart-fragments/

@fradaspa
Copy link

fradaspa commented Apr 6, 2021 via email

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

No branches or pull requests