Stap-voor-Stap Gids: Maak je Eigen Progressive Web App (PWA)


Wat is een Progressive Web App?


Vereisten


Stap 1: Creëer een Manifestbestand

{
  "name": "Mijn PWA App",
  "short_name": "PWA App",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#0d47a1",
  "icons": [
    {
      "src": "/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

<link rel="manifest" href="/manifest.json">
<meta name="theme-color" content="#0d47a1">

self.addEventListener('install', function(event) {
  console.log('Service Worker geïnstalleerd');
});

self.addEventListener('activate', function(event) {
  console.log('Service Worker geactiveerd');
});

self.addEventListener('fetch', function(event) {
  console.log('Fetch request voor:', event.request.url);
});

<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.js')
    .then(function(registration) {
      console.log('Service Worker geregistreerd met scope:', registration.scope);
    })
    .catch(function(error) {
      console.log('Service Worker registratie mislukt:', error);
    });
  }
</script>



const cacheName = 'app-cache-v1';
const filesToCache = [
  '/',
  '/index.html',
  '/styles.css',
  '/script.js',
  '/icons/icon-192x192.png',
  '/icons/icon-512x512.png'
];

// Installeer de service worker en cache bestanden
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(cacheName)
    .then(function(cache) {
      console.log('Bestanden worden gecachet');
      return cache.addAll(filesToCache);
    })
  );
});

// Activeer de service worker en verwijder oude caches
self.addEventListener('activate', function(event) {
  event.waitUntil(
    caches.keys()
    .then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cache) {
          return cache !== cacheName;
        }).map(function(cache) {
          return caches.delete(cache);
        })
      );
    })
  );
});

// Onderschep fetch-verzoeken en serveer gecachte bestanden
self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
    .then(function(response) {
      return response || fetch(event.request);
    })
  );
});

function askPermission() {
  return new Promise(function(resolve, reject) {
    const permissionResult = Notification.requestPermission();
    permissionResult.then(resolve, reject);
  })
  .then(function(permissionResult) {
    if (permissionResult !== 'granted') {
      throw new Error('Toestemming voor notificaties is geweigerd.');
    }
  });
}




Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *