open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

perf(pages): enable webpack code splitting and lazy-load heavy dependencies

Open lizhengfeng101 opened this issue 4 months ago • 3 comments

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

  1. Enable optimization.splitChunks — separate vendor chunks (react, three, mermaid) from application code for parallel loading and long-term caching
  2. Lazy-load Three.js — use React.lazy() + dynamic import() so the 3D background only loads after first paint
  3. Lazy-load Mermaid — dynamically import only on pages/components that render diagrams
  4. Use contenthash in output filenames — e.g., [name].[contenthash:8].bundle.js to enable aggressive CDN caching
  5. Add compression-webpack-plugin — generate pre-compressed .gz / .br files at build time for CDN direct delivery

Acceptance Criteria

  • [ ] bundle.js is 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 build in pages/ succeeds and produces valid output
  • [ ] Site renders correctly with all routes functional (SPA fallback via 404.html still 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).

lizhengfeng101 avatar Jul 23 '26 05:07 lizhengfeng101

/assign

zhuangzhewei09 avatar Jul 23 '26 06:07 zhuangzhewei09

@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:

  1. Webpack output and caching

    • Use content-hashed filename / chunkFilename
    • Enable splitChunks and runtimeChunk
    • Keep index.html and the generated 404.html pointing to the correct hashed entry chunks
  2. 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
  3. 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
  4. Mermaid on actual demand

    • Remove the top-level Mermaid import from MarkdownRenderer
    • Detect language-mermaid blocks 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
  5. Pre-compression and verification

    • Generate both .gz and .br assets with compression-webpack-plugin
    • Run typecheck/build, verify all routes and the 404.html fallback, and provide before/after chunk sizes plus Lighthouse results

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.

zhuangzhewei09 avatar Jul 23 '26 07:07 zhuangzhewei09

Thanks for the thorough breakdown, @chacha923 — your understanding is spot-on.

To confirm your three questions:

  1. 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.
  2. .gz / .br generation — Correct, generating the artifacts in the build is sufficient here. CDN/OSS content-negotiation configuration will be handled separately.
  3. 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 build output served statically (e.g. via npx serve dist) as the "after". Include both screenshots in the PR description so we can compare at a glance.

Looking forward to the PR!

lizhengfeng101 avatar Jul 23 '26 07:07 lizhengfeng101