Fumadocs Editor

Sync

Save to the file system with autosave and live merge

Sync connects the editor to a folder of .md and .mdx files:

  • edits autosave to disk
  • changes on disk (a git pull, a save from your IDE) merge into the open document without moving your caret
  • a block changed on both sides becomes a conflict you resolve in one click

Setup

Install

npm install @fumadocs-editor/core

Add the Vite plugin

It runs the sync server inside vite dev. root is the folder to edit.

vite.config.ts
import { defineConfig } from "vite";
import { editorSync } from "@fumadocs-editor/core/vite";

export default defineConfig({
  plugins: [editorSync({ root: "content/docs" })],
});

Keep Vite away from the edited files

If root sits inside your Vite project, exclude it from the watcher, or every save reloads the page:

vite.config.ts
export default defineConfig({
  server: { watch: { ignored: ["**/content/docs/**"] } },
});

Never import those files as modules either (?raw included).

Point the editor at a file

editor.tsx
import { MdxEditor } from "@fumadocs-editor/ui";

export function SyncedEditor({ path }: { path: string }) {
  return <MdxEditor sync={{ path }} />;
}

The editor loads the file, autosaves and shows a status dot in its header.

Not using Vite? See running the server for Studio and plain Node.

Saving

  • Autosave runs 800 ms after you stop typing, and at most 5 s after the first unsaved change.
  • ⌘S, leaving the window and closing the page save immediately.
  • A save never overwrites a file it has not seen. If the file changed on disk meanwhile, that change is merged in first.
  • When the same block changed on both sides, your version is kept. Saving pauses until you pick keep mine or take disk.
  • Without write access, edits stay in the editor. They are saved once access comes back.

Status

The status dot shows where the document stands:

StatusMeaning
synceddisk matches what you see
dirtyedits waiting for autosave
savinga save is in flight
conflictthe same block changed locally and on disk
offlinedisconnected; edits are kept and saved later
deniedthe server rejected your credentials

To show it elsewhere:

  • sync.onStatus is called with the current status once the document is open (again when path changes), then on every change.
  • useSyncStatus() returns the same status inside MdxEditor.Root.

sync.onWritable works the same way for write access. See Read-only.

Sync client

By default, every editor on the page shares one connection to the dev server. Create your own client to:

Then pass it to every editor:

editor.tsx
import { createSyncClient, wsTransport } from "@fumadocs-editor/core/sync";
import { MdxEditor } from "@fumadocs-editor/ui";

const client = createSyncClient({
  transport: wsTransport("ws://localhost:3100/sync"),
});

export function Editor({ path }: { path: string }) {
  return <MdxEditor sync={{ client, path }} />;
}

Prop

Type

Without MdxEditor

The sync prop is built on client.open. Use it directly for a custom status UI or a non-React shell:

custom-document.tsx
import { useEffect, useRef, useState } from "react";
import { MdxEditor, type MdxEditorRef } from "@fumadocs-editor/ui";
import type { DocumentStatus, SyncClient, SyncDocument } from "@fumadocs-editor/core/sync";

export function CustomDocument({ client, path }: { client: SyncClient; path: string }) {
  const editorRef = useRef<MdxEditorRef>(null);
  const documentRef = useRef<SyncDocument>(null);
  const [status, setStatus] = useState<DocumentStatus>("synced");

  useEffect(() => {
    const editor = editorRef.current!;
    const doc = client.open(path, { editor });
    documentRef.current = doc;
    doc.opened.then(({ text }) => editor.setMarkdown(text), console.error);
    const stop = doc.subscribe(() => setStatus(doc.status()));
    return () => {
      stop();
      doc.close();
    };
  }, [client, path]);

  return (
    <>
      <p>{status}</p>
      <MdxEditor ref={editorRef} onChange={() => documentRef.current?.changed()} />
    </>
  );
}
  • editor is anything with getMarkdown, applyExternalMarkdown, setMarkdown and markSaved. The editor ref qualifies.
  • Load opened's text into the editor, and call changed() on every edit.
  • Read write access from writable(), not from opened: the server answers again after every reconnect. subscribe fires when it changes.
  • A client holds one document per path.

Prop

Type

Last updated on

On this page