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

Rest API batching support (2) #4075

Merged
merged 23 commits into from Apr 20, 2021
Merged

Rest API batching support (2) #4075

merged 23 commits into from Apr 20, 2021

Conversation

mikejolley
Copy link
Member

@mikejolley mikejolley commented Apr 19, 2021

Raising #3899 again.

When testing you must ensure you have reran npm install for the dataloader dependency.

This PR implements batching in the Store API, and a dataloader class in the client to batch requests within a certain interval.

Batching works for POST, PUT, DELETE API requests. GET requests are not affected.

As well as adding the batching, this commit creates a control to handle fragment updates and adds a timeout. This prevents multiple fragment updates after a batch of events have ran.

NOTE: I am requested feedback/code review, but some things are left to do before it can be merged:

  • Check what happens if nonce changes on items in the batch.
  • Batch is only supported on 5.6+ so we need the client to account for this also.
  • Reduce batch timeout from 3000 - I have left this high to make testing easier.

Fixes #2094

How to test the changes in this Pull Request:

  1. Go to the products page.
  2. Add to items to the cart really quickly. If you focus a button with the keyboard you can hit enter and click another button.
  3. Check the network request for a "batch" API call. You'll see 2 responses in an array.
  4. Smoke test cart page functionality/cart/checkout. All should be unaffected.
  5. Check that on the cart page, changing qty persists and the API request is successful.
  6. Check that on the cart page, removing an item from the cart is successful.

Changelog

Added support to the Store API for batching requests. This allows multiple POST requests to be made at once to reduce the number of separate requests being made to the API.

@mikejolley mikejolley requested a review from a team as a code owner April 19, 2021 09:42
@mikejolley mikejolley self-assigned this Apr 19, 2021
@mikejolley mikejolley requested review from Aljullu and removed request for a team April 19, 2021 09:42
@github-actions
Copy link
Contributor

github-actions bot commented Apr 19, 2021

Size Change: +2.41 kB (0%)

Total Size: 1.16 MB

Filename Size Change
build/active-filters.js 8.71 kB +3 B (0%)
build/all-products-frontend.js 35.6 kB +1 B (0%)
build/all-products.js 36.7 kB -254 B (-1%)
build/all-reviews.js 9.92 kB -1 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.95 kB -262 B (-12%) 👏
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 336 B +1 B (0%)
build/atomic-block-components/add-to-cart-frontend.js 8.74 kB -267 B (-3%)
build/atomic-block-components/add-to-cart.js 7.83 kB -12 B (0%)
build/atomic-block-components/button-frontend.js 1.72 kB -299 B (-15%) 👏
build/atomic-block-components/button.js 843 B -53 B (-6%)
build/atomic-block-components/category-list.js 476 B -1 B (0%)
build/atomic-block-components/image-frontend.js 1.82 kB -1 B (0%)
build/atomic-block-components/image.js 1.29 kB -1 B (0%)
build/atomic-block-components/rating.js 525 B +1 B (0%)
build/atomic-block-components/summary.js 926 B +1 B (0%)
build/atomic-block-components/tag-list.js 473 B +1 B (0%)
build/atomic-block-components/title.js 1.26 kB -1 B (0%)
build/blocks-checkout.js 13.7 kB +14 B (0%)
build/cart-frontend.js 77.7 kB -13 B (0%)
build/cart.js 43.6 kB +51 B (0%)
build/checkout-frontend.js 82 kB +3 B (0%)
build/checkout.js 45.9 kB +42 B (0%)
build/handpicked-products.js 6.5 kB -1 B (0%)
build/price-filter.js 10 kB +1 B (0%)
build/price-format.js 1.46 kB +1 B (0%)
build/product-best-sellers.js 6.6 kB +1 B (0%)
build/product-categories.js 3.23 kB -1 B (0%)
build/product-on-sale.js 7.17 kB +1 B (0%)
build/product-search.js 3.6 kB -1 B (0%)
build/product-top-rated.js 6.73 kB +2 B (0%)
build/reviews-by-product.js 13.5 kB +3 B (0%)
build/single-product-frontend.js 38.7 kB +1 B (0%)
build/single-product.js 10.3 kB +2 B (0%)
build/vendors.js 419 kB +19 B (0%)
build/wc-blocks-data.js 10.6 kB +3.27 kB (+44%) 🚨
build/wc-blocks-middleware.js 1.48 kB +160 B (+12%) ⚠️
build/wc-blocks-registry.js 2.75 kB +2 B (0%)
build/wc-settings.js 2.83 kB +2 B (0%)
build/wc-shared-context.js 1.54 kB -3 B (0%)
build/wc-shared-hocs.js 1.72 kB +4 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 8.63 kB 0 B
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 1.8 kB 0 B
build/atomic-block-components/category-list-frontend.js 469 B 0 B
build/atomic-block-components/price-frontend.js 1.98 kB 0 B
build/atomic-block-components/price.js 2 kB 0 B
build/atomic-block-components/rating-frontend.js 521 B 0 B
build/atomic-block-components/sale-badge-frontend.js 861 B 0 B
build/atomic-block-components/sale-badge.js 868 B 0 B
build/atomic-block-components/sku-frontend.js 391 B 0 B
build/atomic-block-components/sku.js 394 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 570 B 0 B
build/atomic-block-components/stock-indicator.js 572 B 0 B
build/atomic-block-components/summary-frontend.js 918 B 0 B
build/atomic-block-components/tag-list-frontend.js 465 B 0 B
build/atomic-block-components/title-frontend.js 1.41 kB 0 B
build/attribute-filter-frontend.js 18.4 kB 0 B
build/attribute-filter.js 12.5 kB 0 B
build/blocks.js 3.49 kB 0 B
build/editor-rtl.css 14.9 kB 0 B
build/editor.css 14.9 kB 0 B
build/featured-category.js 7.87 kB 0 B
build/featured-product.js 10.1 kB 0 B
build/price-filter-frontend.js 14.8 kB 0 B
build/product-category.js 7.57 kB 0 B
build/product-new.js 6.76 kB 0 B
build/product-tag.js 6.61 kB 0 B
build/products-by-attribute.js 7.51 kB 0 B
build/reviews-by-category.js 12 kB 0 B
build/reviews-frontend.js 9.59 kB 0 B
build/style-rtl.css 18.8 kB 0 B
build/style.css 18.8 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 6.54 kB 0 B
build/vendors-style-rtl.css 1.05 kB 0 B
build/vendors-style.css 1.05 kB 0 B
build/wc-blocks-google-analytics.js 1.99 kB 0 B
build/wc-payment-method-bacs.js 812 B 0 B
build/wc-payment-method-cheque.js 807 B 0 B
build/wc-payment-method-cod.js 903 B 0 B
build/wc-payment-method-paypal.js 844 B 0 B
build/wc-payment-method-stripe.js 12.3 kB 0 B

compressed-size-action

@mikejolley mikejolley requested review from nerrad and opr and removed request for Aljullu April 19, 2021 15:29
Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only got a chance to do a review of the code and it looks good to me. I do have a question about removing validate_callback function in favour of an inline nonce check on the response but that is non-blocking.

If needed I can run through some testing tomorrow, but I'll pre-approve based on the code review.

assets/js/middleware/cart-update.ts Outdated Show resolved Hide resolved
src/StoreApi/Routes/AbstractCartRoute.php Outdated Show resolved Hide resolved
tests/e2e/config/jest.setup.js Outdated Show resolved Hide resolved
mikejolley and others added 3 commits April 20, 2021 15:40
Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
@mikejolley mikejolley merged commit fdc74b8 into trunk Apr 20, 2021
@mikejolley mikejolley deleted the add/rest-api-batch-support branch April 20, 2021 15:44
@opr opr added this to the 5.0.0 milestone Apr 21, 2021
@mikejolley mikejolley added type: enhancement The issue is a request for an enhancement. focus: performance The issue/PR is related to performance. labels Apr 28, 2021
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 this pull request may close these issues.

All Products: add to cart can fail when adding multiple products to cart in quick succession
3 participants