mirror of
https://github.com/alexandrev/xslt-lab.git
synced 2026-09-20 04:23:16 +00:00
Perf: defer Monaco mount until browser idle to reduce TBT
All editors were mounting simultaneously on initial render, causing Monaco to block the main thread (TBT 1520ms). Now uses requestIdleCallback (timeout 1.5s) to defer Monaco initialization until after first paint. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+12
-1
@@ -37,6 +37,13 @@ function runWhenIdle(callback, timeout = 2000) {
|
||||
}
|
||||
|
||||
function Editor({ height, ...props }) {
|
||||
const [ready, setReady] = useState(false);
|
||||
useEffect(() => {
|
||||
const id = requestIdleCallback
|
||||
? requestIdleCallback(() => setReady(true), { timeout: 1500 })
|
||||
: setTimeout(() => setReady(true), 300);
|
||||
return () => (requestIdleCallback ? cancelIdleCallback(id) : clearTimeout(id));
|
||||
}, []);
|
||||
const fallbackStyle = height ? { height } : undefined;
|
||||
return (
|
||||
<Suspense
|
||||
@@ -51,7 +58,11 @@ function Editor({ height, ...props }) {
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<MonacoEditor height={height} {...props} />
|
||||
{ready ? (
|
||||
<MonacoEditor height={height} {...props} />
|
||||
) : (
|
||||
<div className="editor-loading" style={fallbackStyle} aria-hidden="true" />
|
||||
)}
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user