पाठ 5 / 29

Offline Support

अपने app को content को cache करके और fallback pages देकर पूरी तरह offline काम करने में सक्षम बनाएँ।

Offline-first Approach

Offline support का मतलब है जब network unavailable हो तो cache से content serve करना। Cached बनाम fresh content serve करने का समय निर्धारित करने के लिए caching strategies का उपयोग करें।

Offline fallback को implement करें

यह fetch handler पहले cache से serve करता है, फिर network से, offline page fallback के साथ।

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(response => response || fetch(event.request))
      .catch(() => caches.match('/offline.html'))
  );
});

Output:

App loads from cache when online, falls back to offline page when unavailable.

एक आम गलती

सब कुछ indefinitely cache न करें। केवल critical assets को cache करें और stale content को update करने के लिए strategies को implement करें।

Building Offline-first Applications

ऐसे apps बनाएँ जो offline पर seamlessly काम करें और reconnect पर sync हों।