Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let cacheName = 'pwa-cache';
- const isValidUrl = urlString => {
- let url;
- try {
- url = new URL(urlString);
- } catch (e) {
- return false;
- }
- if (url.host === 'localhost:3000') {
- return url.protocol === "http:" || url.protocol === "https:";
- }
- return false
- }
- self.addEventListener("install", (event) => {
- event.waitUntil((async () => {
- const cache = await caches.open(cacheName)
- console.log('mulai caching saat install');
- await cache.addAll([
- '/index.html',
- // '/karyawan.html',
- // '/pushnotif.html',
- '/css/materialize.min.css',
- '/js/materialize.min.js',
- '/js/main.js'
- ])
- await self.skipWaiting();
- })());
- });
- self.addEventListener("fetch", (event) => {
- event.waitUntil((async () => {
- if (event.request.url.endsWith(".png") ||
- event.request.url.endsWith(".css") ||
- event.request.url.endsWith(".js") ||
- event.request.url.endsWith(".html")) {
- caches.open(cacheName).then((cache) => {
- // check apakah URL valid atau chrome ekstensi
- if (isValidUrl(event.request.url)) {
- cache.add(event.request)
- }
- })
- }
- event.respondWith(caches.match(event.request)
- .then((resp) => {
- return resp || fetch(event.request);
- }))
- })());
- });
Add Comment
Please, Sign In to add comment