Skip to content

Commit 2ccd43e

Browse files
authored
Lazy-load the Mermaid code block (#4468)
1 parent cd506e3 commit 2ccd43e

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Lazy-load the Mermaid code block so pages that have code blocks but no diagram no longer ship its rendering dependencies.

packages/gitbook/src/components/DocumentView/CodeBlock/CodeBlock.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import type { BlockProps } from '../Block';
1212
import { Blocks } from '../Blocks';
1313
import { ClientCodeBlock } from './ClientCodeBlock';
1414
import { CodeBlockRenderer } from './CodeBlockRenderer';
15-
import { MermaidCodeBlock } from './MermaidCodeBlock';
16-
import { type RenderedInline, getInlines, highlight } from './highlight';
15+
import { MermaidCodeBlockLazy } from './MermaidCodeBlockLazy';
16+
import { highlight } from './highlight';
17+
import { type RenderedInline, getInlines } from './highlight-tokens';
1718

1819
/**
1920
* Render a code block, can be client-side or server-side.
@@ -117,7 +118,7 @@ export async function CodeBlock(
117118
return (
118119
<React.Suspense fallback={null}>
119120
{isMermaid ? (
120-
<MermaidCodeBlock {...clientProps} />
121+
<MermaidCodeBlockLazy {...clientProps} />
121122
) : (
122123
<ClientCodeBlock {...clientProps} />
123124
)}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use client';
2+
3+
import dynamic from 'next/dynamic';
4+
import type { ClientBlockProps } from './ClientCodeBlock';
5+
6+
// Keeps react-aria and @panzoom/panzoom out of the entry chunk for the many pages that have code
7+
// blocks but no diagram. `ssr: true` so real diagram blocks still server-render their skeleton.
8+
const MermaidCodeBlock = dynamic(
9+
() => import('./MermaidCodeBlock').then((mod) => mod.MermaidCodeBlock),
10+
{ ssr: true }
11+
);
12+
13+
export function MermaidCodeBlockLazy(props: ClientBlockProps) {
14+
return <MermaidCodeBlock {...props} />;
15+
}

0 commit comments

Comments
 (0)