Lesson 8.2. Frontend and Core Web Vitals

Goal

Improve LCP, CLS and INP of the store pages and don't load extra JS.

Key concepts

  • LCP — the main product or banner image: size, format, fetchpriority="high", no lazy-load
  • CLS — prices, badges, variations, cookie banners: reserve the space
  • INP — heavy JS on click (variations, filters, add to cart)
  • Cart fragments (wc-cart-fragments) — an AJAX request on every page; in modern versions it loads only with the cart widget
  • Plugin scripts and styles — only on the pages that need them
  • Blocks + Interactivity API — JS only for the blocks on the page

Diagram

flowchart LR
  P["Product page"] --> L["LCP: main photo<br/>fetchpriority=high, WebP/AVIF"]
  P --> C["CLS: variation price,<br/>badges — fixed space"]
  P --> I["INP: variation choice<br/>without heavy handlers"]
  P --> J["JS: only the scripts needed"]

Code

Turn off a plugin's scripts where they aren't needed:

PHP
<?php
add_action( 'wp_enqueue_scripts', static function () {
	if ( ! is_product() ) {
		wp_dequeue_script( 'my-size-chart' );
		wp_dequeue_style( 'my-size-chart' );
	}
}, 100 );

The main product image without lazy-load and with priority:

PHP
<?php
add_filter( 'woocommerce_gallery_image_html_attachment_image_params', static function ( array $params, int $attachment_id, $size, bool $main_image ): array {
	if ( $main_image ) {
		$params['loading']       = 'eager';
		$params['fetchpriority'] = 'high';
	}
	return $params;
}, 10, 4 );

Common mistakes

SymptomCauseFix
A ?wc-ajax=get_refreshed_fragments request on every pageCart fragments from the theme or a pluginFind out what loads them and replace with the Mini-Cart block
CLS when a variation is chosenThe price block changes heightFix the height of the price container
LCP 4+ s on the product pageThe main photo is lazy-loaded and in its original sizeThe right size, a modern format, fetchpriority

Weak and strong answer

Question: How do you improve the LCP of a product page?

Weak answer. Compress the images.

Strong answer. First I find the LCP element in PageSpeed or DevTools — usually the main photo. I check its size (woocommerce_single), the format (WebP/AVIF), that it has no lazy-load and has fetchpriority="high". Next — TTFB: without a fast backend LCP won't be good. I check field data (CrUX), not only the lab.

Why the second is stronger: Diagnosing the element, concrete actions and the difference between lab and field data.

Practice

Run PageSpeed Insights for the home, category and product pages. For each page find the LCP element and the cause of CLS. Fix one problem and compare the result.

Interview Questions

A mechanism that updates the mini cart with the get_refreshed_fragments AJAX request. It made an uncached server request on every page. In modern versions the script loads only where the classic cart widget is.

LCP (how fast the main content appears), INP (how fast the page reacts to actions) and CLS (layout stability).

The lab is one run on an emulated device. Field data (CrUX) comes from real users with different devices, networks and caches.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.