diff --git a/README.md b/README.md index 6e476be1..229c23e7 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ The examples not requiring a backend are now available [via GitHub Pages](https: - [Bad Polyfills](#bad-polyfills) - [buffer](#buffer) - [monaco-editor and react](#monaco-editor-and-react) + - [nextjs](#nextjs) - [Licenses](#licenses) ## Changelogs, project history and compatibility @@ -330,6 +331,146 @@ import { loader } from "@monaco-editor/react"; loader.config({ monaco }); ``` +#### nextjs + +This guide demonstrates how to integrate Language Server Protocol (LSP) functionality with `@monaco-editor/react` in Next.js while avoiding Server-Side Rendering (SSR) issues. + +##### Installation & Setup + +1. **Create Next.js App** + ```bash + bunx create-next-app@latest demo # or equivalent with npm/yarn/pnpm + cd demo + ``` + +2. **Install Dependencies** + Install the required packages with version constraints: + ```bash + bun add @monaco-editor/react monaco-editor@0.36.1 monaco-languageclient@5.0.1 vscode-ws-jsonrpc vscode-languageclient + bun add -D @types/vscode + ``` + > ⚠️ Version Compatibility: + > - `monaco-editor` ≤ 0.36.1 + > - `monaco-languageclient` ≤ 5.0.1 + > - If you prefer not to use the versions specified above, verify version mappings in the [Codingame/Monaco-VSCode API Compatibility Table](https://github.com/TypeFox/monaco-languageclient/blob/main/docs/versions-and-history.md#monaco-editor--codingamemonaco-vscode-api-compatibility-table) + +3. **Start a Language Server** + This guide assumes a C language server running on port `4594`. For a complete setup and configuration reference, check out this [template repository](https://github.com/cfngc4594/monaco-editor-lsp-next) + +##### Implementation + +Replace `src/app/page.tsx` with: + +```typescript +"use client"; + +import { + toSocket, + WebSocketMessageReader, + WebSocketMessageWriter, +} from "vscode-ws-jsonrpc"; +import { useEffect } from "react"; +import dynamic from "next/dynamic"; + +// Lazy-load Monaco Editor with SSR disabled +const Editor = dynamic( + async () => { + await import("vscode"); // Load VSCode type definitions + + const monaco = await import("monaco-editor"); + const { loader } = await import("@monaco-editor/react"); + loader.config({ monaco }); + + return (await import("@monaco-editor/react")).Editor; + }, + { ssr: false, loading: () =>