From 5de314af71e7831f9cd982542751f891368af8e0 Mon Sep 17 00:00:00 2001 From: binayyub4211 Date: Tue, 28 Apr 2026 13:24:41 +0100 Subject: [PATCH] fix: lazy load monaco editor and configure web worker --- src/components/code/AdvancedCodeEditor.tsx | 110 +++++++++++++-------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/src/components/code/AdvancedCodeEditor.tsx b/src/components/code/AdvancedCodeEditor.tsx index 96d49f1e..df843d84 100644 --- a/src/components/code/AdvancedCodeEditor.tsx +++ b/src/components/code/AdvancedCodeEditor.tsx @@ -1,6 +1,8 @@ 'use client'; -import React, { Suspense } from 'react'; +import React from 'react'; +import dynamic from 'next/dynamic'; +import { loader } from '@monaco-editor/react'; import { Play, RotateCcw, @@ -21,10 +23,18 @@ import { AutoCompletion } from './AutoCompletion'; import { CollaborativeEditing } from './CollaborativeEditing'; import type { CompletionSuggestion } from '@/utils/codeUtils'; -// Lazy-load Monaco to avoid SSR issues in Next.js -const MonacoEditor = React.lazy(() => - import('@monaco-editor/react').then((mod) => ({ default: mod.default })), -); +// Configure Monaco Web Worker to use CDN to prevent main-thread blocking +loader.config({ + paths: { + vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.43.0/min/vs' + } +}); + +// Lazy-load Monaco using Next.js dynamic to avoid SSR issues and improve initial render +const MonacoEditor = dynamic(() => import('@monaco-editor/react').then((mod) => mod.default), { + ssr: false, + loading: () => , +}); // --------------------------------------------------------------------------- // Props @@ -42,11 +52,29 @@ interface AdvancedCodeEditorProps { // Sub-components // --------------------------------------------------------------------------- -const EditorLoader: React.FC = () => ( -
-
- - Loading editor… +const EditorSkeleton: React.FC = () => ( +
+
+
+ {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((i) => ( +
+ ))} +
+
+
+
+
+
+
+
+
+
+
+
+
+ + Loading Editor... +
); @@ -239,38 +267,36 @@ export const AdvancedCodeEditor: React.FC = ({ {/* Monaco editor */} {/* ------------------------------------------------------------------ */}
- }> - setCode(val ?? '')} - onMount={handleEditorMount} - options={{ - fontSize, - minimap: { enabled: true }, - wordWrap: 'on', - lineNumbers: 'on', - renderLineHighlight: 'all', - scrollBeyondLastLine: false, - automaticLayout: true, - padding: { top: 12, bottom: 12 }, - suggestOnTriggerCharacters: autoCompleteEnabled, - quickSuggestions: autoCompleteEnabled, - tabSize: languageConfig.id === 'python' ? 4 : 2, - detectIndentation: false, - formatOnPaste: true, - smoothScrolling: true, - cursorBlinking: 'expand', - cursorSmoothCaretAnimation: 'on', - bracketPairColorization: { enabled: true }, - fontFamily: '"Fira Code", "Cascadia Code", "Consolas", monospace', - fontLigatures: true, - }} - height="100%" - width="100%" - /> - + setCode(val ?? '')} + onMount={handleEditorMount} + options={{ + fontSize, + minimap: { enabled: true }, + wordWrap: 'on', + lineNumbers: 'on', + renderLineHighlight: 'all', + scrollBeyondLastLine: false, + automaticLayout: true, + padding: { top: 12, bottom: 12 }, + suggestOnTriggerCharacters: autoCompleteEnabled, + quickSuggestions: autoCompleteEnabled, + tabSize: languageConfig.id === 'python' ? 4 : 2, + detectIndentation: false, + formatOnPaste: true, + smoothScrolling: true, + cursorBlinking: 'expand', + cursorSmoothCaretAnimation: 'on', + bracketPairColorization: { enabled: true }, + fontFamily: '"Fira Code", "Cascadia Code", "Consolas", monospace', + fontLigatures: true, + }} + height="100%" + width="100%" + />
{/* ------------------------------------------------------------------ */}