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/coreAdd the Vite plugin
It runs the sync server inside vite dev. root is the folder
to edit.
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:
export default defineConfig({
server: { watch: { ignored: ["**/content/docs/**"] } },
});Never import those files as modules either (?raw included).
Point the editor at a file
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:
| Status | Meaning |
|---|---|
synced | disk matches what you see |
dirty | edits waiting for autosave |
saving | a save is in flight |
conflict | the same block changed locally and on disk |
offline | disconnected; edits are kept and saved later |
denied | the server rejected your credentials |
To show it elsewhere:
sync.onStatusis called with the current status once the document is open (again whenpathchanges), then on every change.useSyncStatus()returns the same status insideMdxEditor.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:
- connect to another server
- send credentials
- turn on collaboration
Then pass it to every editor:
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:
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()} />
</>
);
}
editoris anything withgetMarkdown,applyExternalMarkdown,setMarkdownandmarkSaved. The editor ref qualifies.- Load
opened'stextinto the editor, and callchanged()on every edit. - Read write access from
writable(), not fromopened: the server answers again after every reconnect.subscribefires when it changes. - A client holds one document per path.
Prop
Type
Last updated on