Home / Fix guides / Reduce unused JavaScript / React

React fix guide

How to reduce unused JavaScript on a React site

A React site often ships one large JavaScript file that contains every page and library, even when a visitor needs only a small part. The fix is to split that file and load the rest only when it is needed.

The "Reduce unused JavaScript" warning in Lighthouse and PageSpeed Insights means the browser downloaded JavaScript that did not run during the page load. On React sites this is usually code for other pages, hidden dialogs, admin screens or big libraries that were bundled into the first download. It slows loading and keeps the browser busy, which hurts Core Web Vitals.

What React does and does not do for you

React does not split your code automatically. Whatever you import at the top of a file ends up in the main bundle unless you say otherwise. Your build tool (Vite, webpack, or a framework like Next.js) can split code, but only where you use dynamic import() or a framework feature built on it.

The build tool does remove some dead code (tree shaking) in production builds, but only when the library is written in a way that allows it. Importing a whole library for one function often defeats this.

This page covers the React side. For the general explanation of the warning, see the general guide to unused JavaScript.

Step by step

  1. Test the production site, not your laptop version. Run Lighthouse in Chrome DevTools or PageSpeed Insights on the live URL. Development builds (npm start or npm run dev) are bigger and will give misleading results. Note which files are listed under "Reduce unused JavaScript".
  1. See what is inside your bundle. Use a bundle visualizer that matches your tool. For Vite, rollup-plugin-visualizer works. For webpack, use webpack-bundle-analyzer. If you only have source maps, source-map-explorer also works. Look for the biggest blocks: a charting library, a rich text editor, a date library, an icon pack, or a whole UI kit.
  1. Split by page (route). This is the biggest win for most sites. Load each page only when a visitor opens it, using React.lazy and Suspense:
import { lazy, Suspense } from 'react';

const Pricing = lazy(() => import('./pages/Pricing'));

<Suspense fallback={<p>Loading...</p>}>
  <Pricing />
</Suspense>

If you use React Router, wrap your route elements this way, or use the router's own lazy loading option if your version has it. In Next.js, pages are already split by route, so focus on step 4 and step 5 instead. Next.js also offers next/dynamic for individual components.

  1. Lazy load heavy parts that are not visible at first. Modals, tabs, chat widgets, maps, video players and editors do not need to load before the visitor clicks. Wrap them in lazy() and render them only when needed.
  1. Import only what you use. Examples:
  • Use import debounce from 'lodash/debounce' instead of importing all of lodash.
  • Replace very large date libraries with a lighter one if you only format dates.
  • Import icons one by one rather than the whole icon package.
  • Check that a library offers an ES module build, since those tree shake better.
  1. Remove packages you no longer use. Search your code for each dependency in package.json. Unused packages are not always bundled, but old imports left in files often are. Run npm uninstall for anything dead.
  1. Handle third party scripts separately. Analytics, chat, ad and tag manager scripts also show up in this warning. Load them with async or defer, load them after the page is interactive, and remove the ones nobody looks at. These live in your index.html or in a tag manager, not in React code.
  1. Build, deploy and test again. Run npm run build, check the file sizes the build prints, deploy, and run Lighthouse again. Compare the same page and the same device setting (mobile or desktop).

A quick way to see unused code in the browser

In Chrome DevTools, open the Coverage panel (search for "Coverage" in the command menu), reload the page and look at the red portions of each file. This shows how much of each file ran. It is a guide, not a target: some code is only needed after a click, which is exactly what lazy loading is for.

What to expect

You will rarely get the warning to zero, and you do not need to. Aim to remove the large, obvious blocks first. Splitting by route and lazy loading one or two heavy components often makes a visible difference. If the page is still slow after that, look at main thread work, since the browser has to run whatever JavaScript remains. You can also check that your files are minified properly with unminified CSS and JS on React.

When the fix is outside your code

  • If the heavy scripts come from a tag manager, the change is made in the tag manager, not in React.
  • If your site was built by an agency or freelancer, ask them to do the route splitting and bundle review. Changing build configuration is safer in the hands of someone who can test it.
  • Create React App is no longer maintained. If your project still uses it, code splitting with lazy() still works, but a move to Vite or a framework may be worth discussing with your developer.

Where SiteMonday fits

SiteMonday runs a speed and Core Web Vitals check in week 4 and lists this warning in a ranked fix list with the written report. It finds and explains the problem; making the change is up to you or your developer.

Not on React? Read the general guide to reduce unused javascript, or the guide for WordPress, Shopify, Wix, Next.js, Google Tag Manager, Elementor.

Questions

Does React.lazy work with server side rendering?

Not on its own in older setups. Frameworks like Next.js provide their own dynamic import tools that handle this, so use those if your site renders on the server.

Will lazy loading make my pages feel slower?

It can, if you lazy load something visible right away, because the visitor waits for the extra download. Split by page and by hidden parts like modals, and keep the main content in the first bundle.

Why does Lighthouse still list a file after I split my code?

Each remaining file may still contain code that did not run during load. Check the bundle visualizer to see whether the file is a library you can import more narrowly or load later.

Is it safe to remove a library from package.json myself?

Only if you have searched your code and confirmed nothing imports it, and you test the built site afterward. If you are unsure, ask your developer.

Do I need to do anything for Next.js specifically?

Next.js already splits code per page, so look for large components and libraries imported at the top of shared files. Use `next/dynamic` for heavy parts that are not needed straight away.

More React fixes