-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
perf: lazy-load Mermaid, YouTube, and TwitterTweetEmbed (#5667, #3186) #5679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lena2099
wants to merge
4
commits into
asyncapi:master
from
lena2099:perf/lazy-load-heavy-components
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1b7dfb
perf: lazy-load Mermaid, YouTube, and TwitterTweetEmbed (#5667, #3186)
lena2099 815e7a8
fix: preserve original Mermaid theme logic, remove sanitize, fix dyna…
lena2099 4d0b933
fix: remove unused React imports, format dynamic() per Prettier
lena2099 fdbf6b5
chore: trigger CI re-run (Lighthouse flake)
lena2099 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| 'use client'; | ||
|
|
||
| import mermaid from 'mermaid'; | ||
| import React, { useEffect, useState } from 'react'; | ||
|
|
||
| type MermaidTheme = 'light' | 'dark'; | ||
|
|
||
| const MERMAID_THEME_VARIABLES: Record<MermaidTheme, Record<string, string>> = { | ||
| light: { | ||
| primaryColor: '#EDFAFF', | ||
| primaryBorderColor: '#47BCEE', | ||
| secondaryColor: '#F4EFFC', | ||
| secondaryBorderColor: '#875AE2', | ||
| fontFamily: 'Inter, sans-serif', | ||
| fontSize: '18px', | ||
| primaryTextColor: '#242929', | ||
| tertiaryColor: '#F7F9FA', | ||
| tertiaryBorderColor: '#BFC6C7', | ||
| lineColor: '#BFC6C7', | ||
| mainBkg: '#EDFAFF', | ||
| secondBkg: '#F4EFFC', | ||
| tertiaryBkg: '#F7F9FA', | ||
| clusterBkg: '#F7F9FA', | ||
| clusterBorder: '#BFC6C7', | ||
| edgeLabelBackground: '#FFFFFF' | ||
| }, | ||
| dark: { | ||
| primaryColor: '#1E293B', | ||
| primaryBorderColor: '#38BDF8', | ||
| secondaryColor: '#2E2459', | ||
| secondaryBorderColor: '#A87EFC', | ||
| fontFamily: 'Inter, sans-serif', | ||
| fontSize: '18px', | ||
| primaryTextColor: '#F8FAFC', | ||
| tertiaryColor: '#121825', | ||
| tertiaryBorderColor: '#475569', | ||
| lineColor: '#94A3B8', | ||
| mainBkg: '#1E293B', | ||
| secondBkg: '#2E2459', | ||
| tertiaryBkg: '#121825', | ||
| clusterBkg: '#121825', | ||
| clusterBorder: '#475569', | ||
| edgeLabelBackground: '#1E293B' | ||
| } | ||
| }; | ||
|
|
||
| // Cache the theme Mermaid was initialized with across client-side page transitions. | ||
| let initializedMermaidTheme: MermaidTheme | null = null; | ||
|
|
||
| /** | ||
| * @description Returns the Mermaid theme that matches the current website theme. | ||
| */ | ||
| function getMermaidTheme(): MermaidTheme { | ||
| if (typeof document === 'undefined') { | ||
| return 'light'; | ||
| } | ||
|
|
||
| return document.documentElement.classList.contains('dark') ? 'dark' : 'light'; | ||
| } | ||
|
|
||
| /** | ||
| * @description Initializes the Mermaid library for the selected theme. | ||
| */ | ||
| function initializeMermaid(theme: MermaidTheme) { | ||
| if (initializedMermaidTheme === theme) { | ||
| return; | ||
| } | ||
|
|
||
| initializedMermaidTheme = theme; | ||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| theme: 'base', | ||
| // Keep Mermaid styling fully controlled by MERMAID_THEME_VARIABLES. | ||
| themeCSS: '', | ||
| themeVariables: MERMAID_THEME_VARIABLES[theme] | ||
| }); | ||
| } | ||
|
|
||
| let currentId = 0; | ||
|
|
||
| /** | ||
| * @description Generates a unique identifier. | ||
| * @returns {string} - A unique identifier. | ||
| */ | ||
| const uuid = (): string => `mermaid-${(currentId++).toString()}`; | ||
|
|
||
| interface MermaidDiagramProps { | ||
| graph: string; | ||
| } | ||
|
|
||
| /** | ||
| * @description This component renders Mermaid diagrams. | ||
| * Extracted from MDX.tsx to enable lazy-loading via next/dynamic, | ||
| * removing ~1.5 MB of Mermaid from the initial JavaScript bundle | ||
| * on pages that don't contain diagrams. | ||
| * | ||
| * @param {MermaidDiagramProps} props - The props for the MermaidDiagram component. | ||
| * @param {string} props.graph - The Mermaid graph to render. | ||
| */ | ||
| function MermaidDiagram({ graph }: Readonly<MermaidDiagramProps>) { | ||
| const [svg, setSvg] = useState<string | null>(null); | ||
| const [theme, setTheme] = useState<MermaidTheme>('light'); | ||
|
|
||
| useEffect(() => { | ||
| setTheme(getMermaidTheme()); | ||
|
|
||
| const observer = new MutationObserver(() => { | ||
| setTheme(getMermaidTheme()); | ||
| }); | ||
|
|
||
| observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); | ||
|
|
||
| return () => observer.disconnect(); | ||
| }, []); | ||
|
|
||
| /** | ||
| * @description Renders the Mermaid diagram. | ||
| */ | ||
| useEffect(() => { | ||
| let mounted = true; | ||
|
|
||
| if (graph) { | ||
| try { | ||
| initializeMermaid(theme); | ||
| mermaid.mermaidAPI.render(uuid(), graph.trim(), (svgGraph) => { | ||
| if (mounted) { | ||
| setSvg(svgGraph); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| if (mounted) { | ||
| setSvg(null); | ||
| } | ||
| // eslint-disable-next-line no-console | ||
| console.error(e); | ||
| } | ||
| } else { | ||
| setSvg(null); | ||
| } | ||
|
|
||
| return () => { | ||
| mounted = false; | ||
| }; | ||
| }, [graph, theme]); | ||
|
|
||
| return <div dangerouslySetInnerHTML={{ __html: svg || '' }} />; | ||
| } | ||
|
|
||
| // Named export mirrors the original inline component name in MDX.tsx. | ||
| // Default export required by next/dynamic. | ||
| export { MermaidDiagram }; | ||
| export default MermaidDiagram; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.