Cumulative Layout Shift in Practice: Crawl a Website to Get a List of All URLs
Hatched by Periklis Papanikolaou
Feb 16, 2024
3 min read
15 views
Cumulative Layout Shift in Practice: Crawl a Website to Get a List of All URLs
Cumulative Layout Shift (CLS) is a metric that measures the visual stability of a webpage as it loads. It quantifies how much elements on a page shift or move unexpectedly, causing a poor user experience. To effectively address CLS issues, it is crucial to have a comprehensive list of all the URLs on a website. In this article, we will explore the process of crawling a website to obtain a complete list of URLs and discuss the importance of addressing CLS in practice.
Crawling a website to gather all URLs is an essential step in understanding the scope of CLS issues. By obtaining a list of all the URLs, web developers and designers can identify the pages that contribute to poor user experience and prioritize their efforts accordingly. There are various methods to crawl a website and retrieve the URLs programmatically.
One popular method involves using the command-line tool wget. By running the appropriate command, wget can recursively crawl a website and output a list of all URLs. For example, the following command will crawl the website "your.website.com" and store the URLs in a file named "urls.txt":
wget --mirror --delete-after --no-directories http://your.website.com 2>&1 | grep '^--' | awk '{print $3}' | sort >urls.txt
This command uses the "--mirror" option to mirror the website, "--delete-after" to remove the downloaded files after the process, "--no-directories" to exclude directories from the output, and redirects the output to a file named "urls.txt".
Another variation of the command can be used to extract only the paths from the URLs, excluding the domain name. This can be useful when analyzing the URLs or when comparing them against a reference list. The modified command is as follows:
wget --mirror --delete-after --no-directories http://your.website.com 2>&1 | grep '^--' | awk '{print $3}' | sort | sed 's/^.//[^/]////' >urls.txt
This command utilizes the "sed" command to remove the domain name from each URL using a regular expression pattern.
Once the complete list of URLs is obtained, it is crucial to analyze each page for potential CLS issues. Cumulative layout shifts can occur due to various reasons, such as images and videos without specified dimensions, dynamically injected content, or asynchronous loading of resources. By identifying these factors and implementing appropriate fixes, web developers can enhance the visual stability of their websites.
One actionable advice to address CLS issues is to ensure that all images and videos have specified dimensions. By explicitly defining the width and height attributes, web browsers can reserve the required space for the media, preventing sudden shifts in the layout as the content loads. This simple practice can significantly improve the user experience and reduce CLS.
Another effective approach is to avoid injecting content dynamically without reserving the necessary space. When content is added to a page asynchronously, it can cause layout shifts if the space for the new content is not reserved beforehand. By reserving the space or using placeholders, web developers can maintain a visually stable layout and mitigate CLS issues.
Lastly, optimizing the loading of resources can also contribute to reducing CLS. Asynchronous loading of resources, such as scripts and stylesheets, can cause delays in rendering the page, leading to unexpected layout shifts. By optimizing the loading process, either by deferring non-critical resources or using techniques like lazy loading, web developers can minimize CLS and provide a smoother user experience.
In conclusion, addressing Cumulative Layout Shift (CLS) in practice requires a comprehensive understanding of the URLs on a website. By crawling a website and obtaining a complete list of URLs, web developers and designers can identify the pages that contribute to CLS issues and prioritize their efforts effectively. Additionally, implementing actionable steps such as specifying dimensions for media, avoiding dynamic content injection without reserved space, and optimizing resource loading can significantly improve the visual stability of a website and enhance the user experience.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣