Skip to content

[Reliability] Offline changes not synced — notes edited offline are lost when coming back online #269

@anshul23102

Description

@anshul23102

Executive Summary

Users who edit notes while offline expect those changes to sync when reconnecting. Currently, offline edits are discarded when the connection is restored.

Root Cause

No offline queue or sync mechanism for note modifications.

Proposed Solution

class OfflineSyncManager {
  constructor(dbName = 'savebook_sync_queue') {
    this.dbName = dbName;
    this.db = null;
  }
  
  async initialize() {
    return new Promise((resolve, reject) => {
      const request = indexedDB.open(this.dbName, 1);
      request.onupgradeneeded = () => {
        const db = request.result;
        db.createObjectStore('sync_queue', { keyPath: 'id', autoIncrement: true });
      };
      request.onsuccess = () => {
        this.db = request.result;
        resolve();
      };
      request.onerror = reject;
    });
  }
  
  async queueNoteChange(noteId, content) {
    const store = this.db.transaction('sync_queue', 'readwrite').objectStore('sync_queue');
    return new Promise((resolve, reject) => {
      const request = store.add({
        noteId,
        content,
        timestamp: Date.now(),
        status: 'pending',
      });
      request.onsuccess = resolve;
      request.onerror = reject;
    });
  }
  
  async syncPendingChanges() {
    const store = this.db.transaction('sync_queue', 'readonly').objectStore('sync_queue');
    return new Promise((resolve, reject) => {
      const request = store.getAll();
      request.onsuccess = async () => {
        const pendingChanges = request.result;
        
        for (const change of pendingChanges) {
          try {
            await fetch(`/api/notes/${change.noteId}`, {
              method: 'PUT',
              body: JSON.stringify({ content: change.content }),
            });
            
            // Remove from queue after successful sync
            await this.removeFromQueue(change.id);
          } catch (error) {
            console.error('Sync failed:', error);
          }
        }
        
        resolve(pendingChanges.length);
      };
    });
  }
}

// Monitor online status and sync
window.addEventListener('online', async () => {
  const syncer = new OfflineSyncManager();
  await syncer.initialize();
  const synced = await syncer.syncPendingChanges();
  console.log(`Synced ${synced} pending notes`);
});

Checklist

  • I have searched existing issues and confirmed this is not a duplicate
  • I have read the CONTRIBUTING.md guidelines
  • I have provided clear steps to reproduce the issue
  • I have described expected vs. actual behavior clearly
  • This issue title is clear and specific
  • This repository has been verified as NSOC on https://www.nsoc.in/projects

@HarshYadav152 Could you please /assign this issue to me? I would like to implement offline sync for note changes under NSOC '26.

/assign

Metadata

Metadata

Assignees

No one assigned

    Labels

    nsoc26Tells us this project is associated with Nexus Spring of Code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions