DeveloperBreeze

Web App Manifest Development Tutorials, Guides & Insights

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

Building Progressive Web Apps (PWAs) with Modern APIs

Tutorial August 05, 2024
json bash

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);
          }
        })
      )
    )
  );
});