Skip to content

Conversation

@mayagbarnes
Copy link
Collaborator

Describe your changes

React's StrictMode exposed some opportunities to improve the recently added lazy loading for StreamlitMarkdown's plugins:rehype-katex, rehype-raw, & remark-emoji

Component is now StrictMode compatible, with improved performance (plugins load once per app, not per component instance), and better handle plugin load failure (render as plain text).

This PR better makes the following changes:

  • Module-level caching: Moved plugin storage outside React's component lifecycle to share across instances and persist across re-renders
  • Adds custom useLazyPlugin hook: Consolidated three nearly-identical useEffect blocks into a single reusable hook with proper cleanup handling
  • Extraction to utils.ts: Moved all plugin-related logic (types, loaders, cache, helpers, hook) to a dedicated file

Testing Plan

  • Unit Tests (JS and/or Python): ✅ Added
  • Manual Testing: ✅

@mayagbarnes mayagbarnes added security-assessment-completed Security assessment has been completed for PR change:refactor PR contains code refactoring without behavior change impact:users PR changes affect end users labels Nov 28, 2025
@snyk-io
Copy link
Contributor

snyk-io bot commented Nov 28, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 28, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13152/streamlit-1.51.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13152.streamlit.app (☁️ Deploy here if not accessible)

@github-actions
Copy link
Contributor

github-actions bot commented Nov 28, 2025

📈 Frontend coverage change detected

The frontend unit test (vitest) coverage has increased by 0.0700%

  • Current PR: 89.1900% (51399 lines, 5555 missed)
  • Latest develop: 89.1200% (51245 lines, 5573 missed)

🎉 Great job on improving test coverage!

📊 View detailed coverage comparison

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the lazy loading mechanism for markdown plugins in StreamlitMarkdown to use module-level caching instead of component-level state. The changes improve React StrictMode compatibility, reduce redundant plugin loads across component instances, and consolidate duplicate loading logic into a reusable hook.

Key Changes

  • Module-level plugin caching: Plugins are now cached at module scope and shared across all component instances, preventing redundant loads
  • Custom useLazyPlugin hook: Consolidates three nearly-identical useEffect blocks into a single reusable hook with proper cleanup and shared in-flight promise handling
  • Extraction to utils.ts: Moves all plugin-related logic (types, loaders, cache, helpers, hook) to a dedicated utilities file with comprehensive test coverage

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
frontend/lib/src/components/shared/StreamlitMarkdown/utils.ts New file containing plugin loading infrastructure: module-level caches, lazy loaders for katex/raw/emoji plugins, plugin extraction logic, error-handling wrappers, and the useLazyPlugin hook
frontend/lib/src/components/shared/StreamlitMarkdown/utils.test.ts Comprehensive test suite for the new utilities covering plugin extraction, wrapper functions, lazy loading hook behavior, cleanup, and error scenarios
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx Refactored to use useLazyPlugin hook instead of three separate useEffect blocks, with wrapped plugins for better error handling

@mayagbarnes mayagbarnes marked this pull request as ready for review November 28, 2025 11:00
Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@mayagbarnes mayagbarnes enabled auto-merge (squash) November 28, 2025 23:33
@mayagbarnes mayagbarnes merged commit 64cb617 into develop Nov 28, 2025
41 of 42 checks passed
@mayagbarnes mayagbarnes deleted the fix-lazy-markdown branch November 28, 2025 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:refactor PR contains code refactoring without behavior change impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants