Neova高级 SEO 平台

JavaScript 文件中未使用的代码超过 Lighthouse 界限

在 Neova 中 开启 Lighthouse 设置后运行:它使用您 Mac 上的 Chrome。

规则最近审核:2026年10月17日 · 指南更新:2026年9月21日

脚本文件中包含页面在加载期间没有运行的代码,访客因此白白下载并解析这些代码。这是 Lighthouse 的界限。没有任何搜索引擎规定它。

您可以做什么

拆分打包文件,让代码在需要时才加载,删除无用代码,并去掉页面不使用的第三方脚本。

Neova 如何发现

Neova 对每类页面抽样运行速度测试。

Neova 为何标记

  • 访客要下载和解析页面在加载期间从不运行的代码,既耗带宽又占主线程时间。
  • 常见原因是体积庞大的打包文件和未使用的第三方脚本。
  • 这是 Lighthouse 的界线;没有搜索引擎规定。

来源

生成的工单

这一部分目前只有英文版。

PE-024 · JavaScript File Is Over the Lighthouse Line for Unused Code

How to Check the Fix: 1. Run the check again in Neova
2. Spot-check sample URLs across affected templates
3. Record the re-check date and period label next to the baseline

Neova writes this ticket for every affected page group and sends it to Jira, Linear, Asana and the rest, after you approve it.

完整指南

这一部分目前只有英文版。

TL;DR, This audit fires when your bundles contain code the page downloads but never executes on load. Dead JavaScript still gets fetched, parsed, and compiled, so it steals network bandwidth and boot-up CPU before a single line of it ever runs, if it runs at all.

What This Check Flags

Lighthouse measures byte-level coverage of every script and reports how much of each file went unused during page load. A 400 KB bundle where only 120 KB executes has 280 KB of pure overhead: bytes the browser fetched, parsed into an AST, and handed to the compiler for nothing. This is a shipping problem, not a runtime one, the fix is to stop sending the code, not to make it run faster.

A Real Example, and the Fix

You import a whole utility library to use one function:

import _ from 'lodash';                 // pulls the entire library into the bundle
const unique = _.uniq(ids);

Even with tree-shaking configured, a default CommonJS import of lodash often drags in the full module because the bundler can't prove the rest is side-effect-free. Import the single function from its own path so only that code lands in the bundle:

import uniq from 'lodash-es/uniq';      // one function, tree-shakeable
const unique = uniq(ids);

For code tied to a screen the user may never open, don't import it statically at all, load it on demand:

button.addEventListener('click', async () => {
  const { openEditor } = await import('./editor.js'); // fetched only on click
  openEditor();
});

The Three Ways Dead Code Gets In

Unused JavaScript almost always traces back to one of these root causes. Naming which one you have points straight at the fix.

Root Cause What It Looks Like The Fix Tool
Whole-library imports import _ from 'lodash' for one helper Import single functions; use ES-module builds Tree-shaking / bundle analyzer
Everything in one bundle Route B's code shipped on Route A Route-based code splitting Dynamic import()
Polyfills for modern browsers Legacy shims served to Chrome/Safari Serve modern JS with module/nomodule Differential serving
Abandoned features Old A/B tests, dead components Delete the source; verify with coverage DevTools Coverage tab

How to Detect It

  1. Lighthouse / PageSpeed Insights: open "Reduce unused JavaScript" under Diagnostics. It lists each script with the wasted bytes and estimated savings, ranked so you fix the biggest offender first.
  2. DevTools Coverage tab: open DevTools, run "Show Coverage" from the command menu, reload, and read the unused percentage per file. Then exercise real interactions, the number should drop as code you actually use finally executes; whatever stays red on a full journey is a deletion candidate.
  3. Bundle analyzer: run webpack-bundle-analyzer or the Vite/Rollup visualizer to see which modules bloat each chunk, so you catch a fat dependency at build time instead of on a live page.

How to Fix It

  1. Turn on tree-shaking properly. Use ES-module imports, mark packages side-effect-free where safe, and build for production so the bundler can drop unreachable exports. Start with the tree shaking primer if the concept is new.
  2. Code-split by route and interaction. Load a view's code only when the user visits it. The code splitting pattern with dynamic import() is the workhorse here.
  3. Import narrowly. Pull single functions from their own paths instead of whole libraries, and prefer smaller dependencies for one-off needs.
  4. Kill dead features. Delete abandoned components and stale experiment code at the source, then re-run Coverage to confirm the bytes are gone.
  5. Defer third parties. Tag managers and widgets are often the biggest chunk of unused JS on load, audit each and load non-essential ones after first paint.

How This Differs From Reduce JavaScript Execution Time

These two are cause and effect, not duplicates. Unused JS is measured in bytes and coverage, how much code you shipped that never ran. Reduce JavaScript execution time is measured in CPU seconds, how long the code that does run takes on the main thread. Deleting unused code helps both audits, because bytes you never send are never parsed or compiled. But you can pass this audit and still fail execution time if the code you do ship runs expensively. Also note the sibling audit Minify JavaScript shrinks the code you keep; removing unused code deletes the code you don't. Do both.

FAQ

Is Unused JavaScript the Same as Unminified JavaScript?

No. Minification compresses the code you keep by stripping whitespace and shortening names. Removing unused code deletes code you never run. They stack: purge the dead code, then minify what survives.

Why Does Tree-Shaking Sometimes Fail to Remove Code?

Tree-shaking relies on static analysis of ES modules. CommonJS imports, dynamic property access, and packages not marked side-effect-free defeat it, so the bundler keeps the code to be safe. Use ES-module builds and a proper sideEffects field to unblock it.

Will Code Splitting Increase the Number of Requests?

Yes, slightly, but over HTTP/2 that trade is almost always a win, because you defer bytes the user may never need instead of blocking the first load with all of them.

How Much Unused JS Is Acceptable?

There is no hard pass line like other audits, but treat anything above roughly 40 KB of wasted script as worth chasing. On mobile, every 100 KB of unnecessary JavaScript is real parse-and-compile time on a slow CPU.

Can I Remove Unused Code From Third-Party Scripts?

Usually not directly, you don't control their bundles. What you can control is whether they load at all and when. Audit each vendor, drop the ones you don't need, and defer the rest past first paint.

在您自己的网站上找到它

Neova 在每次抓取时都会运行此检查项和其他所有检查项,然后把修复方法写成工单交给您的团队。

  • 适用于 Mac、Windows 和 Linux 的桌面应用
  • 全部 696 项检查,每项都附来源
  • Search Console 和 Analytics 4,以只读方式连接
  • 工单发送到 Jira、Linear 和 Asana

下载 Neova Desktop Free 套餐,或免费试用 Solo 14 天,无需银行卡。

全部性能检查 · 指南 · 检查项库

作者:Neova Labs · 返回检查项库