PageSpeed fix guide ·

Legacy JavaScript (Avoid serving legacy JavaScript to modern browsers): how to fix it

Legacy JavaScript means a script on your page carries extra code that only very old browsers need: polyfills and syntax rewritten into older forms. Its sibling, Duplicated JavaScript, means the same module is shipped more than once. The fix is mostly upstream: update or replace the plugin, theme or app that ships the old bundle, build your own code for modern browsers, and load each library once.

In PageSpeed Insights
Legacy JavaScript
Older name
Avoid serving legacy JavaScript to modern browsers
In a FlashMax report
Old or duplicated JavaScript

12.8% of the 40,951 WooCommerce stores FlashMax tested have this problem. See the study

The short fix

  1. Test the page and note which scripts are flagged as legacy or duplicated.
  2. Find the plugin, theme or app behind each file from its address.
  3. Update it; if the latest version is still flagged, ask the vendor for a modern build or replace it.
  4. Make sure each library, such as jQuery or a slider, loads only once.
  5. For your own code, set a modern Browserslist target such as baseline widely available and rebuild.

What PageSpeed Insights is telling you

Legacy JavaScript is listed under Insights in PageSpeed Insights. Before Lighthouse 13 the audit was called Avoid serving legacy JavaScript to modern browsers. The insight fails when the page carries more than 5 KiB of polyfills or transforms for features that Baseline lists as widely available, meaning modern browsers have supported them for a long time.

  • Polyfill: code that re-creates a built-in browser feature for browsers that lack it.
  • Transform: modern syntax that a build tool such as Babel has rewritten into older, longer code.

Duplicated JavaScript is a separate insight: large modules that appear in more than one bundle, or twice in the same bundle. FlashMax puts both into one card, Old or duplicated JavaScript, which appears when a page carries 30 KB or more of either.

Why it slows the page

Modern browsers don’t need this code, but they still download, parse and compile it. Web.dev notes that polyfills pass extra bytes on to every visitor unless they’re loaded only for the browsers that need them, and that the extra JavaScript can add long tasks that block the main thread. That feeds Total Blocking Time (30% of the Lighthouse score) and hurts most on cheaper phones.

A duplicated module costs the same way: the second copy is downloaded and processed for nothing. Both insights show how many bytes are wasted, so you can judge whether a fix is worth the effort.

How common it is

This is one of the less common problems. In our study of 40,951 WooCommerce stores, 12.8% (5,234 stores) carried 30 KB or more of old or duplicated JavaScript on the home page. Their median phone score was 41, against 54 for stores without it. That’s a correlation, not proof: stores with one problem usually have others too. Details: WooCommerce speed study.

How to find the scripts

  • FlashMax: paste your address into the free speed test; the card Old or duplicated JavaScript lists the files behind it.
  • PageSpeed Insights: Insights → Legacy JavaScript lists each script with the polyfills and transforms found in it and the wasted bytes. Insights → Duplicated JavaScript lists modules shipped more than once. The View Treemap button in the Performance section draws your bundles and marks duplicate modules.
  • Chrome DevTools: record a page load in the Performance panel; its insights include Duplicated JavaScript. To spot a library loaded twice, open the Network panel, click the JS filter and look for two copies of the same library from different addresses, for example two jquery.min.js files.

One limit worth knowing: Chrome’s duplicate check reads modules from source maps, the files that map a bundle back to its parts. Scripts served without a source map aren’t checked, so two separate copies of a library may not show up in that insight. The Network panel check catches them.

How to fix it on WordPress and WooCommerce

On WordPress you rarely build the JavaScript yourself. It arrives pre-built inside plugins and themes, so no cache or minify setting can take a polyfill out of someone else’s file: those options change how code is delivered, not what’s in it. The fix is choosing and updating what you install.

  1. Find the owner. /wp-content/plugins/plugin-name/ is a plugin and /wp-content/themes/theme-name/ the theme; other domains are outside services.
  2. Update it. Then re-test and see whether the file is still flagged.
  3. Ask the vendor or replace it. If the latest version still ships polyfills for features every current browser has, tell the vendor: on their side it’s a build setting. If the plugin is no longer maintained, move to one that is.
  4. Load each library once. WordPress already ships jQuery under the script handle jquery. A theme or plugin that adds its own copy from a CDN on top of it doubles the download, and other plugins can end up attached to the wrong copy. The same goes for two slider or lightbox libraries doing the same job: keep one.

If you build your own theme or plugin scripts, set the target in package.json. @babel/preset-env and other tools that read Browserslist then stop adding code for browsers you don’t support:

{
  "browserslist": [
    "baseline widely available"
  ]
}

If your visitors include older browsers, target an earlier Baseline year instead, such as baseline 2020; web.dev notes that even this gives much smaller output than Babel’s default settings. Check which browsers actually visit before you choose; Chrome’s docs point to the Google Analytics Baseline Checker for that.

Shopify and hand-built sites

Shopify: old or duplicated code usually comes from apps or older theme code, which you can’t rebuild yourself. Keep the theme updated, remove apps you don’t use (Shopify notes that uninstalling an app doesn’t automatically remove its code from your theme), and ask app developers whether they ship a modern build. Two apps that each bundle the same library can also cause duplicates; where their features overlap, keep one.

Hand-built sites: set the Browserslist target as above and rebuild. For duplicates, Chrome’s advice is to move large dependencies shared by several bundles into one common bundle with your bundler’s code splitting, and to make sure only one version of each large dependency ends up in a bundle.

Mistakes to avoid

  • Editing a vendor’s file by hand. Stripping polyfills from a plugin’s script can break it, and the next update brings the old file back. Ask the vendor or replace the plugin.
  • Adding a second jQuery “for speed”. One copy is enough; use the one WordPress ships.
  • Changing the browser target blindly. Web.dev’s advice is not to polyfill widely available features unless your visitor data says otherwise. The reverse holds too: check your real visitors before you drop older browsers.
  • Expecting a cache plugin to fix it. Minify, combine and defer don’t remove legacy code; they only change how it’s delivered.

How to check the fix worked

  1. Clear the plugin, server and CDN caches, then re-test two or three times.
  2. Look at the wasted bytes in the Legacy JavaScript and Duplicated JavaScript insights: they should shrink or disappear.
  3. After updating or swapping plugins, open the DevTools Console on the home page, a product page and checkout and look for new errors.
  4. Field data in Search Console and CrUX covers the previous 28 days, so allow about four weeks for it to move.

Questions people ask

What does Legacy JavaScript mean in PageSpeed Insights?

A script includes polyfills or rewritten syntax that only old browsers need, and modern browsers download and process it anyway. Lighthouse flags it when it finds more than 5 KiB of such code for features Baseline lists as widely available.

Can a WordPress plugin fix legacy JavaScript?

Not really. Cache and optimization plugins change how files are delivered, not what’s inside them. The fix is an updated or different plugin or theme, or a modern build if the code is yours.

Why doesn’t PageSpeed flag jQuery loaded twice as duplicated JavaScript?

The Duplicated JavaScript insight finds modules through source maps, so separate copies of a library served without source maps aren’t detected. Use the JS filter in the DevTools Network panel to look for two copies.

Which browsers count as modern?

Lighthouse measures against Baseline: features that have worked in all major browsers for a long time count as widely available. Chrome suggests Baseline Widely available as the default target for new projects, after checking which browsers your visitors actually use.

Sources

  1. Legacy JavaScript (Lighthouse insight) — Chrome for Developers
  2. Duplicated JavaScript (Lighthouse insight) — Chrome for Developers
  3. How to think about Baseline and polyfills — web.dev
  4. Use Baseline with Browserslist — web.dev
  5. ScriptDuplication.ts (duplicate detection source) — Chrome DevTools frontend
  6. Network features reference — Chrome DevTools
  7. wp_enqueue_script() — WordPress Developer Resources
  8. Improving your online store performance — Shopify Help Center

Rather have it done?

The FlashMax team fixes the issues in your report, on a copy first, and shows the before-and-after.

See the speed optimization service

              AMERICANEXPRESSPayPalDISCOVERstripeVISA