Skip to content

Conversation

@mayagbarnes
Copy link
Collaborator

@mayagbarnes mayagbarnes commented Nov 20, 2025

Describe your changes

Attempt to improve initial load time by reducing dependencies included in entry bundle.

  • Lazy load StreamlitSyntaxHighlighter in StreamlitMarkdown
  • Replaced usage of StreamlitSyntaxHighlighter with more minimal StreamlitErrorCodeBlock in ScriptCompileErrorDialog
  • Replaced StreamlitMarkdown in Warning/Connection error dialogs with new DialogErrorMessage

Before:
Page Not Found - before
Bad message format - before
expected_websocket_connection-disconnected_dialog firefox

After:
Page not found - after
Bad message format - after
connection error after

Dependencies removed from entry:

  • highlight.js/lib
  • refractor
  • react-syntax-highlighter

react-syntax-highlighter is a syntax highlighting lib which uses refractor & highlight.js.


Bundle Analysis:

Before (left): 5.11 MB & After (right): 3.51 MB
Entry bundle - after part 1 Entry bundle - after part 2

@snyk-io
Copy link
Contributor

snyk-io bot commented Nov 20, 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.

@mayagbarnes mayagbarnes changed the title Refactor StreamlitSyntaxHighlighter Reduce entry bundle size - Part 2 Nov 20, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 20, 2025

✅ PR preview is ready!

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

Copy link
Collaborator Author

mayagbarnes commented Nov 20, 2025

@mayagbarnes mayagbarnes added 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 labels Nov 20, 2025 — with Graphite App
@github-actions
Copy link
Contributor

github-actions bot commented Nov 20, 2025

📉 Significant wheel size change detected

The wheel file size has decreased by 0.49% (threshold: 0.25%)

  • Current PR: 8781.74 KB
  • Latest develop: 8824.80 KB

Please verify that this change is expected.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 21, 2025

📈 Frontend coverage change detected

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

  • Current PR: 86.1300% (51728 lines, 7173 missed)
  • Latest develop: 86.1000% (51630 lines, 7176 missed)

🎉 Great job on improving test coverage!

📊 View detailed coverage comparison

@github-actions
Copy link
Contributor

github-actions bot commented Nov 23, 2025

📉 Significant bundle size change detected

Metric This Branch develop Change (%)
Total (gzip) 8.21 MiB 8.23 MiB -0.21%
Entry (gzip) 1.01 MiB 1.67 MiB -39.20%

Please verify that this change is expected.

📊 View detailed bundle 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 reduces the entry bundle size by ~1.6MB (from 5.11MB to 3.51MB) through lazy loading of syntax highlighting dependencies and creating lighter alternatives for error dialogs. The key optimization is deferring the loading of react-syntax-highlighter, refractor, and highlight.js until they're actually needed.

Key Changes:

  • Lazy loaded StreamlitSyntaxHighlighter in StreamlitMarkdown with Suspense fallback
  • Created StreamlitErrorCodeBlock as a minimal alternative without syntax highlighting
  • Created DialogErrorMessage component that parses markdown links and plain URLs without full markdown rendering
  • Refactored error handling to use ErrorDetails type with separate message and codeBlock fields

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
frontend/lib/src/index.ts Updated export from StreamlitSyntaxHighlighter to StreamlitErrorCodeBlock
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx Implemented lazy loading for StreamlitSyntaxHighlighter with Suspense and Skeleton fallback
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.test.tsx Updated tests to handle async lazy loading with findBy queries
frontend/lib/src/components/shared/StreamlitMarkdown/Heading.test.tsx Updated tests to wait for lazy-loaded content
frontend/lib/src/components/elements/CodeBlock/StreamlitErrorCodeBlock.tsx New minimal code block component without syntax highlighting for error displays
frontend/lib/src/components/elements/CodeBlock/StreamlitErrorCodeBlock.test.tsx Comprehensive test coverage for the new error code block component
frontend/connection/src/types.ts Added ErrorDetails interface to structure error messages with optional code blocks
frontend/connection/src/WebsocketConnection.tsx Updated to use ErrorDetails instead of plain string error messages
frontend/connection/src/WebsocketConnection.test.tsx Updated all tests to use new ErrorDetails structure
frontend/connection/src/StaticConnection.tsx Updated error handling to use ErrorDetails
frontend/connection/src/DoInitPings.tsx Refactored error messages to separate human-readable text from code blocks
frontend/connection/src/ConnectionManager.ts Updated connection error handling to use ErrorDetails
frontend/app/src/components/StreamlitDialog/styled-components.ts Added StyledErrorText component with conditional spacing
frontend/app/src/components/StreamlitDialog/StreamlitDialog.tsx Replaced StreamlitSyntaxHighlighter with StreamlitErrorCodeBlock in compile error dialog
frontend/app/src/components/StreamlitDialog/DialogErrorMessage.tsx New component for rendering error messages with link parsing, without full markdown
frontend/app/src/components/StreamlitDialog/DialogErrorMessage.test.tsx Comprehensive test coverage for link parsing and error display
frontend/app/src/App.tsx Updated error handling to use ErrorDetails and DialogErrorMessage
frontend/app/src/App.test.tsx Updated all tests to use new ErrorDetails structure

* Parse message text and convert markdown links [text](url) and plain URLs
* into clickable anchor tags.
*/
function parseLinks(text: string): ReactNode[] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: do you have an example case where we render markdown links via the dialog? E.g. do we actually need to support any type of advanced rendering here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We use the link markdown here for retryWhenIsForbidden for CORS documentation, and the ForwardMessageCache errors also use [report this bug](url) so thought it made sense to support this pattern

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, I think it might be good to generate some dedicated parameterized unit test cases for parseLinks as well.

Base automatically changed from reduce-bundle to develop November 24, 2025 20:58
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 👍

* Parse message text and convert markdown links [text](url) and plain URLs
* into clickable anchor tags.
*/
function parseLinks(text: string): ReactNode[] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, I think it might be good to generate some dedicated parameterized unit test cases for parseLinks as well.

@mayagbarnes mayagbarnes merged commit 2a6fd5b into develop Nov 24, 2025
42 checks passed
@mayagbarnes mayagbarnes deleted the reduce-bundle-2 branch November 24, 2025 22:43
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