- Read for 9 minutes
Next.js
E-Commerce
Performance
Case Studies
The most difficult core web issue to fix is Cumulative Layout Shift. This article will discuss the different tools that can be used to examine CLS. We also explain when and how to use them. Finally, it will provide solutions for some CLS problems we encountered in Next.js-based eCommerce website.
Fairprice is one of the largest online grocery stores in Singapore. The company is constantly looking for ways to make online shopping easier. Our users have a wonderful experience regardless of where they are connected to the internet.
There are many key performance indicators (KPI) that measure different points during the lifecycle of the web page (such as TTFB, domInteractiveand onload), but these metrics don’t reflect how the end-user experiences the page.
We wanted to choose KPIs that closely reflect the experience of end-users. This is because if one of these KPIs fails, it can directly affect the user experience. User-centric metrics were the best fit.
There a number of user-centric metrics that can be used to evaluate different stages in the page’s lifetime, such as FCP and LCP. We will be focusing on CLS in this case study.
CLS is a measure of the score for all unanticipated layout changes occurring between the time a page loads and the moment it is finished loading.
Therefore having a low CLS value for a page ensures there are no random layout shifts causing user frustration. Barry Pollard wrote an in-depth article on CLS.
How We Discovered CLS Issue In Our Product Page
We use Lighthouse, WebPagetest and other synthetic tools to test performance for CLS. To measure CLS, we also use web-vitals library. We also check Google Search Console Core Web Vitals Report section for any CLS problems on any pages. While exploring the report section, we found many URLs from the product detail page had more than 0.1 CLS value hinting there is some major layout shift event happening there.
More after jump! Continue reading below |
Now we have identified that the Product Detail Page has a CLS problem, it was time to find the element responsible. We decided to first run tests with synthetic testing tools.
So we ran the lighthouse to check if it could find any element which could be triggering a major layout shift, it reported CLS to .004 which is quite low.
.004. Large preview
The Lighthouse report page contains a section for diagnostics. This section did not reveal any elements that could have caused a high CLS.
Next, we used WebpageTest to verify the view of the filmstrip:
This feature is very useful as it allows us to identify which elements caused the layout shift at any given time. However, we were unable to find any LCS contributing elements when the test was run to check if there had been layout changes.
CLS has a peculiarity: It records layout changes throughout the life of a page, and then adds them.
Note: How CLS is measured has been changed since June 2021.
Since Lighthouse or WebpageTest could not detect any elements that caused a major layout change, it is possible this was occurring after page loading. We decided to install Web Vitals Google Chrome extension because it records CLS while the user interacts with it. We found that the layout shift score increases when users use the image magnify function.
To cross verify if layout shift is occurring while mouseover is on the image, we used the below code snippet from https://web.dev/cls/ which adds console.log when layout shift occurs:
let cls = 0;
new PerformanceObserver((entryList) => {
for (const entry of entryList.getEntries) {
if (!entry.hadRecentInput) {
Cls = entry.value
console.log('Current CLS value:', cls, entry);
|•|||}
}}).observe(type: 'layout-shift', buffered: true);We further investigated and found out that ASDA was facing a similar issue.
Root Cause
When viewing the product detail page users have the option to move their mouse over an image of the product and view a zoomed portion of it side-by side. This video shows you exactly what we’re talking about.
The image magnify function allows our customers to see the appearance and feel of the product, as well as to ensure they are purchasing the correct variant.
We have used react-image zoom library to build this image magnify functionality.
Image Magnify Libraries usually include a lense, which is a square that changes its position when the mouse moves in an image. This lens is being used to trigger CLS because it changes its position by moving the mouse. We checked the library page as well as other similar react libraries (react-image-magnify, react-image-zoom, react-image-magnifiers) and found that all of them are suffering from the same CLS issue.
How We Fixed It
We noticed that react-image-zoom was using the js-image-zoom library. So we had to modify the js-image zoom library to fix the issue.
The solution is quite simple. The solution is quite simple. To fix the problem, we used transform translate which moves the element to a new layer, i.e. Any movement on the new layer does not cause layout shifting:
I have created a PR for the repo to fix the CLS issue so other developers can use this library.
The Impact Of The Change
After the code was deployed to production, the CLS was fixed on the product details page and the number of pages impacted with CLS was reduced by 98%:
transform has a performance advantage over position manipulation using left and top. Large preview
Since we used transform, it also helped to make the image magnify a smoother experience to the users.
Note: Paul Irish has written an excellent article on this topic.
Other Key Changes We Made For CLS
There are other problems we encountered on many pages of our website that contribute to CLS. We also encountered other issues on many pages of our website that contribute to CLS. Let’s take a look at these elements and component and discuss how we attempted to minimize layout changes.
Web-fonts:
Late loading fonts can cause frustrations for users as the content flashes, and sometimes it causes layout changes. We have made a few modifications to minimize the frustration:- We have hosted the fonts ourselves, instead of having them loaded from a third-party CDN.
- We preload fonts.
- We use font-display option.
Images:
The element that follows an image will shift if it has a missing height or width. CLS is prone to this. Since we are using Next.js, we took advantage of the built-in image component callednext/images. It incorporates many image-related best practices. It is built on top ofHTML tag and can help to improve LCP and CLS. This RFC is highly recommended to learn the main features and benefits of its use.Infinite Scroll:
Our product listing pages allow infinite scrolling. This means that layout changes can occur when users scroll down to the bottom of a page. We took a few steps to fix this problem:- The API is used to load data before users reach the bottom.
- We have enough room for the loading stage and show the product skeletons while the loading status is active. Now, the footer is invisible to the user when they scroll.
Addy Olmani wrote a thorough article about this method that I recommend.
Key Takeaways
- While Lighthouse and WebpageTest can detect issues in performance up to page load, it is not possible for them to identify issues later.
- Web Vitals extensions are able to detect CLS changes that have been triggered by user interaction. If a page shows a high CLS but Lighthouse and WebpageTest report low CLS, then Web Vitals can be used to help pinpoint the problem.
- Google Search console data are based on actual users’ data. This data can also point out potential performance issues that may occur at any stage in a page’s lifecycle. After an issue has been identified and corrected, you can check the report section to confirm the effect of the performance improvement. Within days, the changes will be reflected in the web vitals section.
Final Thoughts
While CLS problems are more difficult to diagnose, we can use a variety of tools until page load (Lighthouse and WebPageTest), as well as the Web Vitals extension after page load, in order to pinpoint the problem. This is also a metric that is constantly under development. It can be used to measure a variety of situations. We are following https://web.dev/evolving-cls/ to know about any upcoming changes.
We are constantly improving other Core Web Vitals. Recently, we have implemented responsive image preload and started serving images in WebP format which helped us to reduce 75% of image payload, reduce LCP by 62%, and Speed Index by 24%. For more information on optimizing for speed index and LCP, visit our engineering blog. To learn about the exciting work that we’re doing, you can also read this article.
We would like to thank Alex Castle for helping us debug the CLS issue on the product page and solve the quirks in the next/images implementation.
(vf, yk, il)







