DeveloperBreeze

Modern Web Apis Development Tutorials, Guides & Insights

Unlock 1+ expert-curated modern web apis tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your modern web apis skills on DeveloperBreeze.

Tutorial
json bash

Building Progressive Web Apps (PWAs) with Modern APIs

Open service-worker.js and add the following code to cache static assets:

const CACHE_NAME = 'my-pwa-cache-v1';
const urlsToCache = [
  '/',
  '/index.html',
  '/styles.css',
  '/app.js',
  '/images/icon-192x192.png',
  '/images/icon-512x512.png'
];

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(cache => cache.addAll(urlsToCache))
  );
});

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

self.addEventListener('activate', event => {
  const cacheWhitelist = [CACHE_NAME];
  event.waitUntil(
    caches.keys().then(cacheNames =>
      Promise.all(
        cacheNames.map(cacheName => {
          if (!cacheWhitelist.includes(cacheName)) {
            return caches.delete(cacheName);
          }
        })
      )
    )
  );
});

Aug 05, 2024
Read More