Coverage: Find unused JavaScript and CSS Stay organized with collections Save and categorize content based on your preferences.
TheCoverage panel helps you find unused JavaScript andCSS code. Removing unused code can speed up your page load and save the mobile data of your users.
Overview
Shipping unused JavaScript or CSS is a common problem in web development.For example, suppose that you want to useBootstrap's button componenton your page. To use the button component you need to add a link toBootstrap's style sheet in your HTML, like this:
...<head> ... <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> ...</head>...
This style sheet doesn't just include the code for the button component. Itcontains the CSS forall of Bootstrap's components. But you're not usingany of the other Bootstrap components. So your page is downloading a bunch ofCSS that it doesn't need. This extra CSS is a problem for the followingreasons:
- The extra code slows down your page load. SeeRender-Blocking CSS.
- If a user accesses the page on a mobile device, the extra code uses uptheir mobile data.
TheCoverage panel lets you record your page and view a report of the total used and unused bytes of CSS and Javascript resources, and track the code in theSources panel.
Open the Coverage panel
- Open Devtools.
- Open the Command Menu.
Start typing
coverage
, select theShow Coverage command, and thenpressEnter to run the command. TheCoverage panel opens intheDrawer.
Alternatively, in the top right corner, selectmore_vertMore options >More tools >Coverage.
Record code coverage
To capture code coverage:
To set the coverage scope, in the action bar at the top of theCoverage panel, selectPer function orPer block from the drop-down list.
To start the recording, click
Start instrumenting coverage and reload pageTheCoverage panel reloads the page, captures the code needed to load the page, and continues the recording while you interact with the page.To stop recording code coverage, click
Stop instrumenting coverage and show results.
Analyze code coverage
The table in theCoverage panel shows you what resources were analyzed, and how much code is used within each resource.
Click a row to open that resource in theSources panel and see a line-by-line breakdown of used code and unused code. Any unused lines of code are marked with red lines next to column with line numbers on the left.
- TheURL column is the URL of the resource that was analyzed.
- TheType column says whether the resource contains CSS, JavaScript,or both.
- TheTotal Bytes column is the total size of the resource in bytes.
- TheUnused Bytes column is the number of bytes that weren't used.
- The last, unnamed column is a visualization of theTotal Bytes andUnused Bytes columns. The red section of the bar is unused bytes. Thegray section is used bytes.
To filter the report by URL, specify it in the filter in the action bar.
The status bar at the bottom of theCoverage panel shows you the percentage of code used. The status bar takes filtering into account.
Next to the filter bar, from the drop-down list, you can selectAll, or onlyCSS orJavaScript to show in your report.
To include extension code into your report, turn on
Content scripts.To export your report, click
Export coverage.Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-27 UTC.