perf(pages): enable webpack code splitting and lazy-load heavy dependencies
Description
The landing page at open-codereview.ai currently ships a single monolithic bundle.js of 2.0 MB (uncompressed), with a total site weight of 5.8 MB. All dependencies — including Three.js (3D rendering), Mermaid (diagram rendering), and Marked (markdown parsing) — are bundled into the initial payload regardless of whether the user's viewport needs them.
This makes the site particularly slow for users in regions with high latency to GitHub Pages (e.g., mainland China), where the combination of cross-border routing and large payloads results in 10s+ load times.
Current webpack output config (pages/webpack.config.js):
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js', // no contenthash, no splitChunks
publicPath: '/'
}
Scope
- File(s):
pages/webpack.config.js,pages/src/(component-level lazy loading) - Area: Frontend build pipeline & bundle optimization
Tasks
- Enable
optimization.splitChunks— separate vendor chunks (react, three, mermaid) from application code for parallel loading and long-term caching - Lazy-load Three.js — use
React.lazy()+ dynamicimport()so the 3D background only loads after first paint - Lazy-load Mermaid — dynamically import only on pages/components that render diagrams
- Use contenthash in output filenames — e.g.,
[name].[contenthash:8].bundle.jsto enable aggressive CDN caching - Add
compression-webpack-plugin— generate pre-compressed.gz/.brfiles at build time for CDN direct delivery
Acceptance Criteria
- [ ]
bundle.jsis split into multiple chunks; initial (critical-path) JS is under 400 KB uncompressed - [ ] Three.js and Mermaid are in separate async chunks, loaded on demand
- [ ] Output filenames include content hash for cache-busting
- [ ]
npm run buildinpages/succeeds and produces valid output - [ ] Site renders correctly with all routes functional (SPA fallback via
404.htmlstill works) - [ ] Lighthouse Performance score improves (measure before/after)
Context
This optimization is part of a broader effort to improve site accessibility from mainland China. CDN acceleration (Alibaba Cloud CDN → OSS Hong Kong) addresses network latency, but reducing payload size provides a multiplicative improvement — smaller assets benefit more from edge caching and parallel downloads.
Current largest chunks (if split were enabled):
| Dependency | Approx. size |
|---|---|
| Three.js | ~658 KB |
| Mermaid | ~422 KB |
| React + ReactDOM | ~145 KB |
| Marked | ~60 KB |
| App code | ~200 KB |
Target: first-paint critical path ≤ 300 KB (React + app shell + CSS).
/assign
@lizhengfeng101 Thanks for assigning this to me. I reviewed the current main implementation and would like to confirm the intended scope before starting.
My proposed breakdown is:
-
Webpack output and caching
- Use content-hashed
filename/chunkFilename - Enable
splitChunksandruntimeChunk - Keep
index.htmland the generated404.htmlpointing to the correct hashed entry chunks
- Use content-hashed
-
Route-level code splitting
- Lazy-load non-landing routes such as Docs, Blog, Benchmark, and Quick Start
- Keep the landing shell/critical content synchronous so the initial route does not pull in all Markdown content and documentation UI
-
Three.js after first paint
- Lazy-load
ColorBends - Mount it after the initial paint/idle callback, with a stable CSS fallback to avoid layout shift or a blank hero
- Lazy-load
-
Mermaid on actual demand
- Remove the top-level Mermaid import from
MarkdownRenderer - Detect
language-mermaidblocks first, then dynamically import and initialize Mermaid only when a rendered document contains a diagram - Keep Marked/DOMPurify available through the lazy Docs/Blog route chunks
- Remove the top-level Mermaid import from
-
Pre-compression and verification
- Generate both
.gzand.brassets withcompression-webpack-plugin - Run typecheck/build, verify all routes and the
404.htmlfallback, and provide before/after chunk sizes plus Lighthouse results
- Generate both
Could you please confirm these scope details?
- Is route-level lazy loading expected/acceptable as part of reaching the <400 KB initial-JS target?
- For
.gz/.br, is generating the artifacts sufficient for this issue, with CDN/OSS content-negotiation configuration handled separately? - Should the Lighthouse comparison be measured against the public production site and included in the PR description, or is there a preferred environment/profile?
Once confirmed, I’ll implement this breakdown and report the measured bundle changes.
Thanks for the thorough breakdown, @chacha923 — your understanding is spot-on.
To confirm your three questions:
- Route-level lazy loading — Yes, it's in scope for this issue. SplitChunks alone likely won't hit the <400 KB target without route-level splitting.
.gz/.brgeneration — Correct, generating the artifacts in the build is sufficient here. CDN/OSS content-negotiation configuration will be handled separately.- Lighthouse comparison — Please run Lighthouse (Desktop, default throttling) against the current production site (open-codereview.ai) as the "before" baseline, then against your local
npm run buildoutput served statically (e.g. vianpx serve dist) as the "after". Include both screenshots in the PR description so we can compare at a glance.
Looking forward to the PR!