Duplicated JavaScript: find the second copy and remove it

Why Lighthouse finds two copies of React or lodash, why Wix sites show it and WordPress sites hardly ever do, and how to load each library once.

Arjen Karel
Updated · 6 min read
Share

Why does Lighthouse say your JavaScript is duplicated?

Duplicated JavaScript, called Remove duplicate modules in JavaScript bundles before Lighthouse 13, means two of your scripts contain the same package, such as React or lodash, so every visitor downloads and runs it twice. It is unscored. Build the scripts that share a library together, so the library is in one file; on Wix, use fewer Wix apps on a page.

Just want the quick fix? Jump to Wix, WordPress and Shopify or your own build.

How does Lighthouse find duplicated JavaScript?

Lighthouse reads each script's source map, a file next to the minified script that lists the packages in it, and counts a package in the maps of two scripts as a duplicate. The notice reads: "Remove large, duplicate JavaScript modules from bundles to reduce unnecessary bytes consumed by network activity".

Lighthouse report, Duplicated JavaScript in orange with Est savings of 70 KiB, the description and the labels FCP, LCP and Unscored; a table with Source and Duplicated bytes: node_modules/react-dom 40 KiB and node_modules/lodash 20 KiB, then date-fns, react and scheduler at 0 KiB, each with configurator.js showing two dashes and site.js showing the bytes

A sofa shop's test page in a Lighthouse mobile report, with the simulated throttling PageSpeed Insights uses: React DOM and lodash in both scripts.

Each row under Source is a package (node_modules/react-dom is React DOM) or one source file, by its path. Under it are the scripts that contain it. Lighthouse counts the largest copy as the original and shows two dashes next to it; the Duplicated bytes of the other scripts are the extra bytes your visitors download. Rows are rounded to 10 KiB, so a small package shows 0 KiB and still counts in Est savings. Est savings with an empty table usually means two versions of one package inside one script.

Red means Lighthouse estimates that the extra bytes delay your first paint (FCP) or your largest element (LCP), usually through a script in the head; orange means it only counts the bytes. Unscored: your Performance score stays the same, but your visitors still download every copy.

How much slower is a page with two copies of React?

The part of the page that needs the duplicated scripts appears later: on a shop, the product options and the Add to basket button. The browser downloads, compiles and runs every copy: each file is a separate script.

I built a sofa shop's product page with a React configurator in two ways: the header and the configurator as two separate builds, each with its own React, React DOM and lodash, and as one build that shares a single copy. Both loaded on a phone connection of 1.6 Mbps with 150 ms round trips, with the CPU slowed four times:

BuildJavaScript (gzip)Configurator on screenScripts running
Two separate builds160 kB1.91 s141 ms
One build, one copy86 kB1.46 s58 ms
The sofa shop's product page on a phone with the two separate builds: the green sofa photo, the name The Harbour 3-seater sofa, the price and the rating, and below them grey blocks where the configurator goes

Before

The same page at the same moment with one build: under the price and the rating the configurator with the sizes 2-seater, 3-seater, 4-seater and Corner, and the fabric swatches

After

1.5 seconds into the load: grey blocks where the configurator goes, or the size and fabric choices.

The configurator appeared 0.45 s later. Lighthouse showed the notice orange: it estimated 0 ms of delay for the first paint and the photo. The scripts still download at the same time as the sofa photo and run on the same main thread.

Where does the second copy come from?

View Treemap, a button in the Lighthouse report and in PageSpeed Insights, shows each script as a box with its packages inside, by size. A package in two boxes is a copy.

Lighthouse Treemap of the sofa page: two boxes side by side, /assets/js/before/site.js 77.8 KiB and /assets/js/before/configurator.js 77.7 KiB, each filled mostly by react-dom (about 42 KiB) and lodash/lodash.js (about 23 KiB), both outlined, and a small date-fns box

View Treemap in the same report: the outlined boxes, React DOM and lodash, are in both files.

  • Two separate builds: the usual cause. A build puts every package its code imports into its own files, so two scripts built separately, by two teams for example, each contain their own React.
  • Two versions of one package: two of your dependencies require lodash version ranges that don't overlap, and npm installs both. npm ls lodash lists every copy with its version.
  • The same file twice: two script tags load one file. The browser downloads it once and runs it twice, and the report shows the same file name on both lines.
  • Wix: Wix's own files and each Wix app, such as Bookings or Forms, bundle their own copies of the same libraries, and Wix publishes the source maps. Every Wix site I tested showed the notice in orange, from 22 to 138 KiB.
  • A plugin, app or vendor widget: listed only when its files have a source map, which is rare. That is why the notice hardly ever shows on WordPress or Shopify.

For your own code, a bundle analyser (webpack-bundle-analyzer, rollup-plugin-visualizer) shows the same boxes straight from the build.

Why does jQuery loaded twice pass this notice?

Lighthouse only compares scripts that have a source map, and most library, theme and plugin files have none. Those are often where the real duplicates are: a theme and a plugin that each load their own jQuery, or two slider plugins that each bundle the same slider library. A passed notice tells you nothing about them.

Find them in DevTools: open the Network panel, filter on JS and reload. Look for the same library name twice, or one file loaded from two addresses (your own server and a CDN) or with two different ?ver= numbers. Then keep one copy.

How do you fix Duplicated JavaScript?

Pick the largest row whose files you build or choose. The domain and path under each package tell you whose script it is: static.parastorage.com is Wix, /wp-content/plugins/ is a plugin, and your own domain is usually your own build.

Wix

The listed files are Wix's own. You control how many Wix apps a page uses, and each one brings its own copies: remove the apps and widgets a page doesn't need. The highest number I measured, 138 KiB, was on a page with four Wix apps. Leave the rest: only Wix can change it.

WordPress and Shopify

  • Update first: a new version may share the library with the rest of the page.
  • Keep one of two plugins that bring the same library, such as two sliders.
  • Load it on fewer pages: on WordPress, unload the plugin's script per page with Asset CleanUp or Perfmatters; on Shopify, turn off the embed of an app you don't use (Online Store, Themes, Customize, App embeds) or ask the vendor to load the widget only on the templates that show it.
  • A library loaded twice: ask the developer of the plugin, app or theme that brings the second copy to use the copy already on the page. Don't unload either copy with these tools: WordPress then also drops every script that needs it. A second copy is a sign that many plugins or apps each load their own scripts. Remove those you don't need. After you install a new one, check the Network panel for a second copy.

Combining or minifying JavaScript in Asset CleanUp or LiteSpeed Cache clears the notice, because it removes the line that points to the source map. Both copies still download.

Your own build

Turn two separate builds into one build with two entries, and let the bundler move the packages they share into one chunk. In webpack:

// webpack.config.js
module.exports = {
  entry: {
    site: './src/site.jsx',
    configurator: './src/configurator.jsx'
  },
  optimization: {
    splitChunks: { chunks: 'all' },
    runtimeChunk: 'single'
  }
};

With runtimeChunk: 'single' the page gets one webpack runtime, so a shared module runs once. The page then loads the runtime and the shared file too; HtmlWebpackPlugin adds those script tags. Vite and Rollup share chunks between the entries of one build by default, and Next.js does this for you.

For two versions, npm ls lodash shows which dependency requires which version, and npm dedupe merges the copies whose version ranges overlap. When they don't, update the dependency that requires the old version, or force one version with overrides in package.json (resolutions in Yarn) and test what uses it: a package forced onto another major version can break.

When two script tags load the same file, remove one. For a package the page doesn't use at all, see Reduce unused JavaScript.

Run Lighthouse again and the notice reads "No duplicated JavaScript found". Hiding the source maps, with hidden-source-map in webpack or 'hidden' in Vite, gives the same result while both copies still download, so also check that the total JavaScript in the Network panel went down.

When is a duplicate too small to fix?

Leave an orange row of a few KiB, a vendor's file you need and can't replace, and Wix's own files once each page has only the apps it needs. Fix a framework or a large library in two of your own bundles (tens of KiB, like React DOM on the sofa page), and fix every red notice: there the extra bytes delay your first paint or your LCP.

A page built from several apps and widgets, each with its own framework, needs a decision on which build gives up its copy and how the others load the shared one. We settle that in a power hour, with your own report and Treemap on screen.

About the author

Arjen Karel is a web performance consultant and the creator of CoreDash, a Real User Monitoring platform that tracks Core Web Vitals data across hundreds of sites. He also built the Core Web Vitals Visualizer Chrome extension. He has helped clients achieve passing Core Web Vitals scores on over 925,000 mobile URLs.

Arjen Karel

Need a faster site?

I'm Arjen, and I've been fixing slow sites for 17 years. If you're running into issues you can't fix yourself, send me your URL and let's have a chat.

Send me your URL
Arjen Karel
Arjen Karel, Web performance consultant since 2009

Why isn't our fix working?

Sometimes the CDN still serves the old HTML, or the fix went into the desktop template and mobile uses another one. Send me your URL and the problem, and I reply with a few times. I check your field data first, then we spend one hour on your site, and within a day you have my notes in writing. €350 excl. VAT.

Send me your URL

No developer to build the fix? I do the work myself, on a copy of your site.

Arjen KarelCore Web Vitals still red on mobile?Send me your URL