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

  1. Mock mode was enabled (useMockApi: true) which disabled sync
  2. Each browser created local-only spaces without backend sync
  3. 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

  1. Check Console - Should see:

    [SyncEngine] ✅ Initialized
    [SyncEngine] 🔄 Starting sync...
    [SyncEngine] ✅ Sync complete
    
  2. No Errors - Should NOT see:

    • "Unknown entity type: undefined"
    • "Failed to sync operation"
  3. Queue Should Be Clean:

    [SyncEngine] ✅ Sync complete
    duration: "0ms", queueSize: 0  // ✅ Queue is empty!
    

Step 5: Test Cross-Browser Sync

  1. Browser A: Login → Create/view space → Note space ID
  2. Browser B: Login with SAME user → Should see SAME space!
  3. Create Universe in Browser A → Should sync to backend
  4. 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

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

  1. Initial Setup: App was in mock mode (useMockApi: true)
  2. Sync Disabled: Mock mode disables SyncEngine
  3. Local-Only Spaces: Each browser created its own space in IndexedDB
  4. Mode Switch: When switched to real mode, old queue data had undefined values
  5. Sync Errors: Corrupted queue items caused sync failures

The Fix

Architecture

This follows the documented Offline-First Architecture:

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