Skip to content

perf(editor): add lazy viewport loading for MonacoEditorCore and ReadOnlyCodeBlock - #3804

Closed
Raing5Days wants to merge 1 commit into
openBitFun:mainfrom
Raing5Days:perf/monaco-pool-opt
Closed

Raing5Days wants to merge 1 commit into
openBitFun:mainfrom
Raing5Days:perf/monaco-pool-opt

Conversation

@Raing5Days

Copy link
Copy Markdown
Contributor

Summary of Changes

  • Viewport intersection lazy loading: Added \lazy\ prop support to \MonacoEditorCore\ and enabled it by default in \ReadOnlyCodeBlock.
  • Eliminate offscreen editor instantiation: When code blocks are rendered outside the readable viewport (e.g. in conversation history, collapsed tools, or documentation previews), creation of the heavy Monaco editor instance (\monaco.editor.create), DOM nodes, and event listeners is deferred until the block intersects within 200px of the viewport.
  • Graceful SSR/environment fallback: Automatically falls back to immediate instantiation when \IntersectionObserver\ is unavailable.

Verification

  • Unit tests:
    • Added \ReadOnlyCodeBlock.test.tsx\ covering deferred instantiation under \lazy={true}\ until intersection and immediate instantiation when \lazy={false}.
    • \CodeEditor.test.tsx: 15/15 tests passed.
    • Regression verified with full editor test suite.

@wsp1911

wsp1911 commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for working on this. ReadOnlyCodeBlock currently has no production callers, so this change won’t reduce rendering work in conversation history, tool cards, or Markdown previews. Conversation code blocks use MarkdownRenderer, which already defers Prism highlighting based on viewport visibility for long Markdown sections.

I’m closing this PR for now. If a production view starts using ReadOnlyCodeBlock and profiling shows measurable overhead from creating offscreen Monaco instances, we can revisit this approach for that view.

@wsp1911 wsp1911 closed this Nov 14, 2026
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.

2 participants