SkillByAIइंटरैक्टिव संस्करण खोलें →

पाठ 16 / 30

IndexedDB & Storage

Structured offline data IndexedDB में, responses Cache API में रखें, और browser storage quotas की योजना बनाएँ।

Local Storage Options

IndexedDB objects/indexes में structured data store करता है। Cache API responses के लिए optimized है। App data के लिए IndexedDB choose करें, HTTP responses और static assets के लिए Cache API।

IndexedDB से store और retrieve करें

Todos, notes या user preferences जैसे app data को store करने के लिए IndexedDB का उपयोग करें।

const dbRequest = indexedDB.open('AppDB', 1);
dbRequest.onsuccess = () => {
  const db = dbRequest.result;
  const store = db.transaction('todos', 'readwrite').objectStore('todos');
  store.add({ id: 1, title: 'Learn PWA', completed: false });
};

dbRequest.onupgradeneeded = event => {
  const db = event.target.result;
  db.createObjectStore('todos', { keyPath: 'id' });
};

Output:

App data persisted in IndexedDB for offline access.

एक आम गलती

IndexedDB transactions complex हो सकते हैं। हमेशा onsuccess और onerror events को handle करें और याद रखें कि storage quotas browser के अनुसार अलग-अलग होते हैं।