If you want to update this page or add new content, please submit a pull request to the Homepage.
Revisions
Revisions provide version control for Yorkie documents, allowing you to save snapshots at specific points in time, browse history, and restore previous states. This guide explains how to integrate revision features into your application.
Overview
Revisions are immutable snapshots of a document's state stored on the server. Each revision contains:
- Label: A user-friendly identifier (e.g., "v1.0", "Draft", "Final")
- Description: Optional detailed explanation of what changed
- Snapshot: The complete document state in YSON format
- Timestamp: When the revision was created
- Server Sequence: The document's server sequence number at creation time
Use Cases
- Version History: Allow users to browse and restore previous document versions
- Milestone Tracking: Save important states before major changes
- Audit Trail: Maintain a record of document evolution over time
- Collaboration Review: Preview changes before accepting them
- Rollback: Recover from unwanted changes or errors
Before You Begin
This guide assumes familiarity with:
- Client and Document basics
- YSON format for understanding snapshots
How Revisions Work
When you create a revision:
- The server builds the current document state
- The state is serialized to YSON format (data only, no CRDT metadata)
- The revision is stored with metadata in the database
- A
RevisionSummaryis returned to the client
API Reference
For detailed API documentation, see JS SDK - Document Revisions.
| Method | Description |
|---|---|
client.createRevision(doc, label, description?) | Create a new revision |
client.listRevisions(doc, options?) | List revisions with pagination |
client.getRevision(doc, revisionId) | Get revision details with snapshot |
client.restoreRevision(doc, revisionId) | Restore document to a revision |
Building a Revision UI
This section shows how to build a revision management interface, using patterns from CodePair as a reference.
React Hook (Recommended)
@yorkie-js/react provides a built-in useRevisions hook that wraps the client's revision API and automatically binds the current client and document. Use it inside a DocumentProvider:
import { useState, useEffect } from 'react';import { useRevisions } from '@yorkie-js/react';import type { RevisionSummary } from '@yorkie-js/react';function RevisionPanel() {const { createRevision, listRevisions, getRevision, restoreRevision } =useRevisions();const [revisions, setRevisions] = useState<RevisionSummary[]>([]);useEffect(() => {listRevisions({ pageSize: 20, isForward: false }).then(setRevisions);}, [listRevisions]);const handleCreate = async () => {const label = `v${new Date().toISOString().split('T')[0]}`;await createRevision(label, 'Manual checkpoint');setRevisions(await listRevisions({ pageSize: 20 }));};const handlePreview = async (revisionId: string) => {const revision = await getRevision(revisionId);// Display revision.snapshot in a preview dialog};const handleRestore = async (revisionId: string) => {if (confirm('Restore to this revision? Current state will be replaced.')) {await restoreRevision(revisionId);setRevisions(await listRevisions({ pageSize: 20 }));}};return (<div><button onClick={handleCreate}>Save Revision</button><ul>{revisions.map((rev) => (<li key={rev.id}><span>{rev.label}</span><span>{new Date(rev.createdAt).toLocaleString()}</span><button onClick={() => handlePreview(rev.id)}>Preview</button><button onClick={() => handleRestore(rev.id)}>Restore</button></li>))}</ul></div>);}
The hook returns four methods:
| Method | Description |
|---|---|
createRevision(label, description?) | Create a named revision snapshot |
listRevisions(options?) | List revisions with pagination (pageSize, offset, isForward) |
getRevision(revisionID) | Get a revision with its full snapshot |
restoreRevision(revisionID) | Restore the document to a previous revision |
For the full hook API reference, see React SDK - useRevisions.
For a complete working example, see the react-revision example.
Manual Approach
If you need custom loading/error state management or aren't using DocumentProvider, you can build a custom hook using the client API directly:
1import { useState, useCallback, useEffect } from 'react';2import { Client, Document, RevisionSummary } from '@yorkie-js/sdk';34export function useCustomRevisions(client: Client | null, doc: Document<any, any> | null) {5 const [revisions, setRevisions] = useState<RevisionSummary[]>([]);6 const [isLoading, setIsLoading] = useState(false);78 const fetchRevisions = useCallback(async () => {9 if (!client || !doc) return;10 setIsLoading(true);11 try {12 const revs = await client.listRevisions(doc, { pageSize: 50, isForward: false });13 setRevisions(revs);14 } finally {15 setIsLoading(false);16 }17 }, [client, doc]);1819 const createRevision = useCallback(async (label: string, description?: string) => {20 if (!client || !doc) throw new Error('Client or document not available');21 setIsLoading(true);22 try {23 const revision = await client.createRevision(doc, label, description);24 await fetchRevisions();25 return revision;26 } finally {27 setIsLoading(false);28 }29 }, [client, doc, fetchRevisions]);3031 useEffect(() => {32 if (client && doc) fetchRevisions();33 }, [client, doc, fetchRevisions]);3435 return { revisions, isLoading, createRevision, fetchRevisions };36}
Previewing Snapshots
Use the YSON parser to convert snapshots into readable data:
1import { YSON } from '@yorkie-js/sdk';23async function previewRevision(revisionId: string) {4 const revision = await client.getRevision(doc, revisionId);56 if (revision.snapshot) {7 // Parse YSON to get document data8 const data = YSON.parse<YourDocType>(revision.snapshot);910 // For Text fields, convert to plain string11 if (data.content) {12 const plainText = YSON.textToString(data.content);13 console.log('Document content:', plainText);14 }15 }16}
Restoration Flow
When restoring a revision, the server:
- Loads the revision's YSON snapshot
- Creates a new document state from the snapshot
- Applies the change through the CRDT merge process
- Syncs the restored state to all connected clients
Restoring replaces the entire document state. Consider creating a new revision before restoring to preserve the current state as a backup.
Auto-Revisions
Yorkie can automatically create revisions during snapshot creation. This is controlled by the auto-revision-enabled project setting.
| Setting | Default | Description |
|---|---|---|
auto-revision-enabled | true | Automatically create revisions during snapshots |
When enabled:
- Revisions are created automatically during garbage collection snapshots
- Labels follow the format:
snapshot-{serverSeq} - Descriptions:
Auto created revision of snapshot #{serverSeq}
Configure via the Dashboard or Admin API. See Resources for details.
Best Practices
When to Create Revisions
- Before major edits: Save state before significant changes
- At milestones: Mark completion of features or sections
- Periodically: For long editing sessions, create time-based checkpoints
- Before restore: Always save current state before restoring an old revision
Labeling Conventions
Use consistent, descriptive labels:
| Pattern | Example | Use Case |
|---|---|---|
| Version numbers | v1.0, v2.1 | Release milestones |
| Date-based | 2024-01-15, backup-2024-01-15 | Time-based checkpoints |
| Feature names | auth-complete, refactor-done | Feature milestones |
| Descriptive | before-review, final-draft | Workflow stages |
Storage Considerations
- Revisions store full document snapshots, not deltas
- Each revision consumes storage proportional to document size
- Use auto-revisions judiciously for large documents
- Consider periodic cleanup of old revisions if storage is a concern
Permissions
Revision operations require appropriate document permissions:
| Operation | Required Permission |
|---|---|
| Create | Write access |
| List | Read access |
| Get | Read access |
| Restore | Write access |
Related Documentation
- JS SDK - Document Revisions - Complete API reference
- React SDK - useRevisions - React hook for revision management
- YSON - Understanding snapshot format
- Resources - Project configuration including auto-revisions
- CodePair - Reference implementation