Skip to content
Performance & Accessibility

How to Get Unused CSS and JavaScript Right

Code downloaded by a page that is never used there, commonly from frameworks, component libraries or site-wide bundles.

MediaScaleUp Editorial Team 2 min read 13 views
How to Get Unused CSS and JavaScript Right

Where most projects go wrong

The usual mistakes:

  • One global bundle for every page
  • Loading entire component libraries for a few components
  • Keeping code for retired features
  • Removing CSS without checking dynamic class names

What good looks like instead

  • Check coverage on key templates
  • Split bundles by route or feature
  • Purge unused framework CSS during the build
  • Remove dead features and their code
  • Track bundle sizes in continuous integration
What to do and what to avoid with unused CSS and JavaScript, side by side
Good practice against the usual mistakes, from the sources listed below.

Why it matters

Unused code still costs download and processing time, and it usually accumulates quietly as features are added and removed.

The specs that matter

Published figures for Unused CSS and JavaScript
MeasureFigure
Coverage tabChrome DevTools shows the share of each file that is unused
Lighthouse auditsReduce unused JavaScript and Reduce unused CSS
Common sourcesCSS frameworks and site-wide bundles
Code splittingloads code only for the pages or features that need it

Knowing when to hand it over

Tip: Bring in help when bundles have grown over years, or when coverage shows most of the code is unused.

Where this comes from

The figures and practices above come from the sources listed.

Working on something like this?

We take on Performance & Accessibility work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Performance & Accessibility