From 907b96d4800ae9a969c13dc3f4e45aef52aba931 Mon Sep 17 00:00:00 2001 From: Jeff Emmett Date: Sun, 16 Nov 2025 21:00:18 -0700 Subject: [PATCH] fix: use proper Automerge URL format for repo.find() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The issue was that repo.find() was creating a NEW document instead of waiting for the existing one to sync from the network. Changes: - Use 'automerge:{documentId}' URL format for repo.find() - Remove try-catch that was creating new documents - Let repo.find() properly wait for network sync This ensures all clients use the SAME document ID and can sync in real-time. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/automerge/useAutomergeSyncRepo.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/automerge/useAutomergeSyncRepo.ts b/src/automerge/useAutomergeSyncRepo.ts index 12f13b5..fbd99d0 100644 --- a/src/automerge/useAutomergeSyncRepo.ts +++ b/src/automerge/useAutomergeSyncRepo.ts @@ -3,7 +3,7 @@ import { TLStoreSnapshot } from "@tldraw/tldraw" import { CloudflareNetworkAdapter } from "./CloudflareAdapter" import { useAutomergeStoreV2, useAutomergePresence } from "./useAutomergeStoreV2" import { TLStoreWithStatus } from "@tldraw/tldraw" -import { Repo } from "@automerge/automerge-repo" +import { Repo, parseAutomergeUrl, stringifyAutomergeUrl } from "@automerge/automerge-repo" import { DocHandle } from "@automerge/automerge-repo" interface AutomergeSyncConfig { @@ -146,18 +146,11 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus let handle: DocHandle if (documentId) { - // Try to find the existing document - console.log(`🔍 Attempting to find document ${documentId}`) - try { - handle = await repo.find(documentId as any) - console.log(`✅ Found document handle: ${documentId}`) - } catch (error) { - // Document not available yet - this can happen when it exists on server but not locally - console.log(`📝 Document ${documentId} not immediately available, creating new handle`) - // Create a new handle - the sync will handle merging with server state - handle = repo.create() - console.log(`📝 Created new handle ${handle.documentId}, will sync with server`) - } + // Convert document ID to Automerge URL format and find it + console.log(`🔍 Finding document ${documentId} via network sync`) + const docUrl = `automerge:${documentId}` as const + handle = await repo.find(docUrl as any) + console.log(`✅ Got handle for document: ${handle.documentId}, isReady: ${handle.isReady()}`) } else { // Create a new document and register its ID with the server handle = repo.create()