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

Conversation

@mikejolley
Copy link
Member

This PR implements resource hinting for cart and checkout scripts when we can predict the customer will be visiting those pages during the current session. It essentially pre-caches block assets in the background when the page is idle so that when the customer lands on the cart and checkout pages, the block scripts can be loaded from cache.

Fixes #2207

cc @senadir

See:

The new rules include:

  1. Cart has contents, not on the cart page = Load cart block assets
  2. Cart has contents, not on checkout page = Load checkout assets
  3. Cart has contents, viewing the cart page = Prerender the checkout page

Case 3 uses the prerender attribute which is not supported in all browsers, but is by Chrome. It does not actually render the page or run JS, but it does pre-load all assets and resources on the page. See https://developers.google.com/web/updates/2018/07/nostate-prefetch

Testing

Manual Testing

How to test the changes in this Pull Request:

  1. Clear website cache.
  2. Go to the homepage with nothing in your cart.
  3. View source. Look for rel='prefetch' rules. Cart and checkout will not be there.
  4. Go to the shop page and add something to the cart.
  5. Go back to the homepage.
  6. View source. Look for rel='prefetch' rules:

Screenshot 2022-01-12 at 13 17 57

If you open network tools at this point and then visit the cart page, you should see the majority of scripts are loaded from cache.

User Facing Testing

Users cannot see these changes in an obvious way, so smoke testing should be performed only of the checkout flow.

Performance Impact

For first time visitors to the cart and checkout pages, this should increase the page loading times due to assets being retrieved from cache rather than the server.

Changelog

Add resource hinting for cart and checkout blocks to improve first time performance.

@mikejolley mikejolley added focus: performance The issue/PR is related to performance. block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. status: needs review labels Jan 12, 2022
@mikejolley mikejolley self-assigned this Jan 12, 2022
@rubikuserbot rubikuserbot requested review from a team and senadir and removed request for a team January 12, 2022 13:20
@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2022

Size Change: 0 B

Total Size: 864 kB

ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 6.28 kB
build/active-filters.js 6.97 kB
build/all-products-frontend.js 18.6 kB
build/all-products.js 34 kB
build/all-reviews.js 8.03 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/catego--90468e1a.js 223 B
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.64 kB
build/atomic-block-components/add-to-cart-frontend.js 7.01 kB
build/atomic-block-components/add-to-cart.js 7.49 kB
build/atomic-block-components/button--atomic-block-components/category-list--atomic-block-components/imag--f11cdc7a.js 501 B
build/atomic-block-components/button-frontend.js 2.08 kB
build/atomic-block-components/button.js 2.3 kB
build/atomic-block-components/category-list-frontend.js 922 B
build/atomic-block-components/category-list.js 500 B
build/atomic-block-components/image-frontend.js 1.86 kB
build/atomic-block-components/image.js 1.08 kB
build/atomic-block-components/price-frontend.js 1.95 kB
build/atomic-block-components/price.js 1.51 kB
build/atomic-block-components/rating-frontend.js 1.14 kB
build/atomic-block-components/rating.js 717 B
build/atomic-block-components/sale-badge-frontend.js 1.1 kB
build/atomic-block-components/sale-badge.js 683 B
build/atomic-block-components/sku-frontend.js 386 B
build/atomic-block-components/sku.js 386 B
build/atomic-block-components/stock-indicator-frontend.js 1.04 kB
build/atomic-block-components/stock-indicator.js 623 B
build/atomic-block-components/summary-frontend.js 1.34 kB
build/atomic-block-components/summary.js 923 B
build/atomic-block-components/tag-list-frontend.js 923 B
build/atomic-block-components/tag-list.js 498 B
build/atomic-block-components/title-frontend.js 1.31 kB
build/atomic-block-components/title.js 933 B
build/attribute-filter-frontend.js 16.8 kB
build/attribute-filter.js 13.1 kB
build/blocks-checkout.js 17.4 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.16 kB
build/cart-blocks/checkout-button-frontend.js 1.16 kB
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/express-payment-frontend.js 5.2 kB
build/cart-blocks/filled-cart-frontend.js 767 B
build/cart-blocks/items-frontend.js 298 B
build/cart-blocks/line-items-frontend.js 5.5 kB
build/cart-blocks/order-summary-frontend.js 8.88 kB
build/cart-blocks/totals-frontend.js 321 B
build/cart-frontend.js 45.3 kB
build/cart.js 43.6 kB
build/checkout-blocks/actions-frontend.js 1.41 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.13 kB
build/checkout-blocks/billing-address-frontend.js 891 B
build/checkout-blocks/contact-information-frontend.js 2.84 kB
build/checkout-blocks/express-payment-frontend.js 5.5 kB
build/checkout-blocks/fields-frontend.js 345 B
build/checkout-blocks/order-note-frontend.js 1.13 kB
build/checkout-blocks/order-summary-frontend.js 11.3 kB
build/checkout-blocks/payment-frontend.js 7.78 kB
build/checkout-blocks/shipping-address-frontend.js 997 B
build/checkout-blocks/shipping-methods-frontend.js 4.73 kB
build/checkout-blocks/terms-frontend.js 1.22 kB
build/checkout-blocks/totals-frontend.js 325 B
build/checkout-frontend.js 47.5 kB
build/checkout.js 44.8 kB
build/featured-category.js 8.63 kB
build/featured-product.js 9.73 kB
build/handpicked-products.js 7.11 kB
build/legacy-template.js 2.19 kB
build/mini-cart-component-frontend.js 16.5 kB
build/mini-cart-contents-block/empty-cart-frontend.js 328 B
build/mini-cart-contents-block/filled-cart-frontend.js 230 B
build/mini-cart-contents-block/footer--mini-cart-contents-block/products-table-frontend.js 5.33 kB
build/mini-cart-contents-block/footer-frontend.js 5.67 kB
build/mini-cart-contents-block/items-frontend.js 225 B
build/mini-cart-contents-block/products-table-frontend.js 5.36 kB
build/mini-cart-contents-block/shopping-button-frontend.js 288 B
build/mini-cart-contents-block/title-frontend.js 367 B
build/mini-cart-contents.js 23.6 kB
build/mini-cart-frontend.js 1.72 kB
build/mini-cart.js 6.33 kB
build/price-filter-frontend.js 12.5 kB
build/price-filter.js 8.49 kB
build/price-format.js 1.19 kB
build/product-best-sellers.js 7.39 kB
build/product-categories.js 3.17 kB
build/product-category.js 8.5 kB
build/product-new.js 7.68 kB
build/product-on-sale.js 8 kB
build/product-search.js 2.19 kB
build/product-tag.js 7.83 kB
build/product-top-rated.js 7.92 kB
build/products-by-attribute.js 8.4 kB
build/reviews-by-category.js 11.5 kB
build/reviews-by-product.js 12.6 kB
build/reviews-frontend.js 7.34 kB
build/single-product-frontend.js 22 kB
build/single-product.js 10 kB
build/stock-filter-frontend.js 6.5 kB
build/stock-filter.js 6.58 kB
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 19 kB
build/vendors--atomic-block-components/add-to-cart-frontend.js 7.51 kB
build/vendors--atomic-block-components/price--cart-blocks/line-items--cart-blocks/order-summary--checkout--194c50bf-frontend.js 5.26 kB
build/vendors--cart-blocks/line-items--checkout-blocks/order-summary--mini-cart-contents-block/products-table-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 4.74 kB
build/vendors--mini-cart-contents-block/footer--mini-cart-contents-block/products-table-frontend.js 7.71 kB
build/wc-blocks-data.js 9.83 kB
build/wc-blocks-editor-style-rtl.css 4.84 kB
build/wc-blocks-editor-style.css 4.84 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 953 B
build/wc-blocks-registry.js 2.7 kB
build/wc-blocks-shared-context.js 1.52 kB
build/wc-blocks-shared-hocs.js 1.14 kB
build/wc-blocks-style-rtl.css 22.2 kB
build/wc-blocks-style.css 22.2 kB
build/wc-blocks-vendors-style-rtl.css 1.28 kB
build/wc-blocks-vendors-style.css 1.28 kB
build/wc-blocks-vendors.js 69.6 kB
build/wc-blocks.js 2.62 kB
build/wc-payment-method-bacs.js 816 B
build/wc-payment-method-cheque.js 811 B
build/wc-payment-method-cod.js 909 B
build/wc-payment-method-paypal.js 837 B
build/wc-settings.js 2.61 kB

compressed-size-action

@mikejolley mikejolley removed the request for review from senadir February 24, 2022 16:42
@mikejolley mikejolley marked this pull request as draft February 24, 2022 16:42
@mikejolley mikejolley marked this pull request as ready for review February 24, 2022 16:42
@mikejolley mikejolley requested review from a team and ralucaStan and removed request for a team February 24, 2022 16:43
@ralucaStan ralucaStan requested a review from a team March 4, 2022 15:11
Copy link
Contributor

@alexflorisca alexflorisca left a comment

Choose a reason for hiding this comment

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

Nice., hopefully this will speed up the checkout process a fair bit! Would be interested to see by how much and if we can measure it somehow. I'll have a think.

The code looks good, again with the caveat that I'm not great at PHP. Wonder how long I can get away with that for haha

When I tried running it in the browser, it looks like the cart isn't loading properly. I can see the fetch to the cart store (wc/store/v1/cart?_locale=user) fails with a 404. That's even when the resources are loaded normally, not from prefetch cache, so I'm not sure why...

@mikejolley mikejolley force-pushed the add/cart-checkout-resource-hints branch from f92502a to cb8fa84 Compare March 9, 2022 10:25
@alexflorisca alexflorisca self-requested a review March 9, 2022 12:14
Copy link
Contributor

@alexflorisca alexflorisca left a comment

Choose a reason for hiding this comment

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

Nice, thanks for fixing the 404, looks good :)

@github-actions github-actions bot added this to the 7.2.0 milestone Mar 9, 2022
@mikejolley mikejolley modified the milestones: 7.2.0, 7.3.0 Mar 9, 2022
@mikejolley mikejolley merged commit 6970b23 into trunk Mar 9, 2022
@mikejolley mikejolley deleted the add/cart-checkout-resource-hints branch March 9, 2022 12:25
@tjcafferkey tjcafferkey added the type: enhancement The issue is a request for an enhancement. label Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

block: cart Issues related to the cart block. block: checkout Issues related to the checkout block. 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.

Implement resource hinting between Cart and Checkout blocks

4 participants