Space Synchronization Fix - Clear Corrupted Queue
How to fix corrupted sync queue causing "undefined entity" errors
🔧 Space Synchronization Fix
Problem
Different browsers showing different spaces for the same user, with console errors:
[SyncEngine] ⚠️ Unknown entity type: undefined - skipping sync
[SyncEngine] ❌ Failed to sync operation
Root Cause
- Mock mode was enabled (
useMockApi: true) which disabled sync - Each browser created local-only spaces without backend sync
- Corrupted queue data persisted from before sync was properly enabled
Solution
Step 1: Clear Persisted Data
Open DevTools Console and run:
// Clear the corrupted sync queue
localStorage.removeItem('flash:sync-queue');
// Clear old space data (optional - only if you want fresh start)
// localStorage.clear();
// Reload the page
location.reload();
Step 2: Verify Environment Configuration
Ensure apps/gograb-space/src/environments/environment.ts has:
export const environment = {
production: false,
// ✅ CRITICAL: Disable mock mode to enable sync
useMockApi: false, // Must be FALSE!
// ✅ Use local backend (or railway for production)
backendApiUrl: BACKEND_URLS.local, // http://localhost:3000/api
deploymentMode: DEPLOYMENT_MODES.development,
...SHARED_CONFIG,
};
Step 3: Restart Application
# Stop the running server (Ctrl+C)
# Start fresh
npx nx serve gograb-space
Step 4: Verify Sync is Working
Check Console - Should see:
[SyncEngine] ✅ Initialized [SyncEngine] 🔄 Starting sync... [SyncEngine] ✅ Sync completeNo Errors - Should NOT see:
- "Unknown entity type: undefined"
- "Failed to sync operation"
Queue Should Be Clean:
[SyncEngine] ✅ Sync complete duration: "0ms", queueSize: 0 // ✅ Queue is empty!
Step 5: Test Cross-Browser Sync
- Browser A: Login → Create/view space → Note space ID
- Browser B: Login with SAME user → Should see SAME space!
- Create Universe in Browser A → Should sync to backend
- Check Backend: Space should be in MongoDB with correct data
How Sync Works Now
User Action (Create/Update)
↓
SpaceService.write()
↓
SyncEngine.write()
↓
├─→ IndexedDB (instant, < 10ms) ✅
└─→ Sync Queue (background)
↓
(5 second interval)
↓
Push to Backend API
↓
MongoDB (synced!) ✅
Verification Checklist
-
useMockApi: falsein environment.ts - Backend API running on port 3000
- Cleared
localStorage.removeItem('flash:sync-queue') - Console shows "✅ Sync complete" without errors
-
queueSize: 0after sync completes - Same user sees same space in different browsers
Expected Console Output (Good)
[SyncEngine] ✅ Initialized
enabled: true
queueSize: 0
[SyncEngine] 🔄 Starting sync...
queueSize: 0
[SyncEngine] ✅ Sync complete
duration: "2ms"
queueSize: 0
If Still Having Issues
Check Backend Connection
// In DevTools Console
fetch('http://localhost:3000/api/health')
.then(r => r.json())
.then(console.log);
// Should return: { status: "ok", ... }
Check Auth Token
// In DevTools Console
const token = localStorage.getItem('auth_backup');
console.log('Auth token:', token ? 'EXISTS' : 'MISSING');
Manual Sync Trigger
// In DevTools Console
window.__spaceService__.getStatus();
// Should show sync status
Technical Details
Why This Happened
- Initial Setup: App was in mock mode (
useMockApi: true) - Sync Disabled: Mock mode disables SyncEngine
- Local-Only Spaces: Each browser created its own space in IndexedDB
- Mode Switch: When switched to real mode, old queue data had undefined values
- Sync Errors: Corrupted queue items caused sync failures
The Fix
- Clear Queue: Remove corrupted persisted data
- Enable Sync: Set
useMockApi: false - Fresh Start: Clean slate with proper sync enabled
Architecture
This follows the documented Offline-First Architecture:
- See:
docs/english/GoGrab-Space/plans/lambo-builder/storage-offline-first/FINAL_ARCHITECTURE_SUMMARY.md - All operations hit local storage first (fast!)
- Background sync to MongoDB (automatic!)
- Works offline, syncs when online
Success Criteria
✅ Console shows no sync errors
✅ Same user sees same space in all browsers
✅ Spaces persist to MongoDB backend
✅ Sync queue stays at queueSize: 0 after sync
✅ Operations complete in < 10ms locally
Date: November 17, 2025
Status: ✅ Solution Verified
Next: Test in production with Railway backend