首页 > 解决方案 > 为什么 Google Page Speed Insights 说要缩小 JS

问题描述

我运行了 Google Page Speed Insights,他们的主要建议是缩小js文件,节省超过 70%(准确地说是从 423 KB 到 312 KB),这太疯狂了。

但是文件被缩小了!我错过了什么?

资源:

PS我已经解决了关于SO的其他类似问题,但都与我的问题不相似。

标签: javascriptminifygoogle-pagespeedpagespeed-insights

解决方案


我建议浏览您的缩小文件并寻找人类可读的 JS 令牌。

据我所知,测试主要是看 JS 令牌长度。

https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/audits/byte-efficiency/unminified-javascript.js

/**
 * @fileOverview Estimates minification savings by determining the ratio of parseable JS tokens to the
 * length of the entire string. Though simple, this method is quite accurate at identifying whether
 * a script was already minified and offers a relatively conservative minification estimate (our two
 * primary goals).
 *
 * This audit only examines scripts that were independent network requests and not inlined or eval'd.
 *
 * See https://github.com/GoogleChrome/lighthouse/pull/3950#issue-277887798 for stats on accuracy.
 */

如果您在缩小文件中看到人类可读的标记,请尝试查看您的缩小器设置,看看是否有可以切换的选项来消除这些标记。

如果没有设置可以消除它们,请尝试确定它们的来源以及它们与其他成功缩小的令牌有何不同。罪魁祸首可能是编码模式、框架或转译器。

注意:源代码链接自他们的帮助页面(https://web.dev/unminified-javascript/


推荐阅读