Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Performance: Only load cart-fragments.js when needed. #7168

Closed
nerrad opened this issue Sep 16, 2022 · 1 comment · Fixed by #7644
Closed

Performance: Only load cart-fragments.js when needed. #7168

nerrad opened this issue Sep 16, 2022 · 1 comment · Fixed by #7644
Labels
focus: performance The issue/PR is related to performance. type: enhancement The issue is a request for an enhancement.

Comments

@nerrad
Copy link
Contributor

nerrad commented Sep 16, 2022

When a store is using the Cart and Checkout blocks and the Mini Cart Block, I'm assuming there is no need to load the cart-fragments.js script. Currently, that script loads on every page and has a performance hit on stores (because of the refresh Ajax requests). I did a really brief experiment disabling the loading of the script on a store that has all the above blocks active and as far as I can tell, nothing was broken. However, this will require more extensive testing on any implementation.

Given articles like this out there, as well as solutions like this, I think it'd be a great optimization to implement this. Especially given the mini-cart block already has lazy-loading in place for performance.

@nerrad nerrad added type: enhancement The issue is a request for an enhancement. focus: performance The issue/PR is related to performance. labels Sep 16, 2022
@mikejolley
Copy link
Member

Cart Fragments is designed to update static displays of cart items (e.g. lists of items in the cart, cart totals, etc etc) rendered by themes, plugins, and core (namely, the cart widget).

There are 2 events that trigger a fragment request (found in core's add_to_cart.js script) which are:

  • added_to_cart
  • removed_from_cart

These occur on the "shop" page where there are AJAX powered add to cart buttons. I believe the other place these events can occur is from product shortcodes.

Removing fragments has no effect on blocks, just legacy things.

Given the existence of the mini cart, I think we can remove our handling, in blocks, for triggering fragment requests. Mini Cart should be the preferred method of showing carts going forward. This is part 1 of the issue. By removing this, we'll be able to reduce AJAX requests on the page, even if the fragments script is present on the page.

Part 2 of the issue is how to safely dequeue the fragment script for performance reasons. This might not be possible without a core change. We'd also need to work out under which circumstances if it safe to dequeue. Until the core Shop page is replaced with blocks, this might not yet be possible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
focus: performance The issue/PR is related to performance. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants