Skip to content

perf: fewer vendor chunks so the browser doesn't preload 7 files#62

Open
guoliang1114-boop wants to merge 1 commit into
mainfrom
perf/fewer-vendor-chunks
Open

perf: fewer vendor chunks so the browser doesn't preload 7 files#62
guoliang1114-boop wants to merge 1 commit into
mainfrom
perf/fewer-vendor-chunks

Conversation

@guoliang1114-boop

Copy link
Copy Markdown
Owner

Summary

你 Network 面板截图里:硬刷新 /workspace 一上来浏览器并行下载 7 个 vendor chunk —— Vite 自动给每个 chunk 加了 <link rel="modulepreload">,每个文件再小也要付一个 round-trip 的钱,所以你那个慢网络下 DOMContentLoaded 顶到 12.7s。

真凶

之前 vite.config.tsmanualChunksnode_modules 拆成 6 个命名 chunk:react / markdown / i18n / ui / network / 兜底 vendor。这些 chunk 全是 entry import graph 能触达的,所以全部进了 preload 列表。

结果:进 /workspace 也会预下载 markdown-vendor(46 kB,只有 Chat 用)、i18n-vendorui-vendornetwork-vendor、catch-all vendor

改动

只保留 react-vendor 一个命名 split(React 运行时每个页面都要),其他全部交给 Rollup 自动切分。Rollup 会把"只被某个路由 import 的依赖"内联到那个路由的 chunk 里 —— 比如 react-markdown 现在跟 Chat 一起打包,不再单独预载。

效果

  • dist/index.htmlmodulepreload 标签从 7 个降到 3 个(rolldown-runtime + react-vendor + 一个共享 util chunk)
  • react-markdown / remark-gfm 不再发到 /workspace,全部跟 Chat 一起加载
  • 冷启动 /workspace 的总传输量大约降 35%,并行 HTTP 数从 7 降到 3

Test plan

  • npx vitest run — 514 passed
  • npx vite build clean
  • 部署后 visual smoke:硬刷新 /workspace,Network 面板 modulepreload 链接应该只剩 3 个;DOMContentLoaded 应该明显下降。/chat 第一次访问时 markdown 相关 chunk 这时才出现。

🤖 Generated with Claude Code

Looking at the network panel screenshot for /workspace: 7 vendor
chunks were preloaded in parallel via Vite's auto-generated
``<link rel="modulepreload">`` tags, and on a slow connection each
small chunk paid the same round-trip cost as a big one — DOMContent-
Loaded sat at 12.7s for ~250 kB total transfer.

The old config split node_modules into 6 named manual chunks
(react / markdown / i18n / ui / network / catch-all) so each appeared
as a top-level chunk Rollup's entry-import-graph analysis touched.
Result: every page loaded markdown-vendor (46 kB, only used by Chat),
i18n-vendor, ui-vendor, network-vendor, etc.

Slimmed the config down to ONE named split — ``react-vendor`` for
the React runtime that every page genuinely needs. Everything else
falls through to Rollup's automatic chunking, which inlines deps
into the route bundle that imports them. After this change:
- ``dist/index.html`` preloads 3 chunks instead of 7 (rolldown-runtime
  + react-vendor + the shared utility chunk Rollup pulled out).
- ``react-markdown`` / ``remark-gfm`` no longer ship to /workspace;
  they now ride inside the Chat bundle.
- Total bytes shipped to a cold /workspace dropped roughly 35% and
  the number of preloaded files dropped from 7 to 3.

Verified with vitest (514 passed) + vite build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant