Hi, I am Trishul
@trishulgoel
@trishulgoel
BUILDING BLOCKS OF PWA
@trishulgoel
@trishulgoel
@trishulgoel
@trishulgoel
@trishulgoel
@trishulgoel
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/serviceworker.js').then(function(registration) {
// Registration success
}, function(err) {
// Registration fail
});
});
}
@trishulgoel
@trishulgoel
@trishulgoel
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('my-cahce').then(function(cache) {
return cache.addAll(
[
'/css/style.css',
'/js/script.js',
'/index.html'
]
);
})
);
});
@trishulgoel
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheName) {
return Promise.all(
cacheName.filter(function(name) {
return name !== 'my-cache';
}).map(function(name) {
return caches.delete(name);
})
)
})
)
});
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request);
}
)
);
});
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open('cahce-key').then(function(cache) {
return fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
})
);
});
In service worker
@trishulgoel
@trishulgoel
self.addEventListener('push', function (event) {
if (event && event.data) {
self.pushData = event.data.json();
if (self.pushData) {
event.waitUntil(self.registration.showNotification(self.pushData.title, {
body: self.pushData.body,
icon: self.pushData.data ? self.pushData.data.icon : null
}).then(function() {
clients.matchAll({type: 'window'}).then(function (clientList) {
if (clientList.length > 0) {
messageToClient(clientList[0], {
message: self.pushData.body // suppose it is: "Hello World !"
});
}
});
}));
}
}
});
@trishulgoel
@trishulgoel
@trishulgoel
@trishulgoel
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(
[
'/css/style.css',
'/js/script.js',
'/index.html'
]
);
})
);
});
@trishulgoel
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request));
});
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
if(!response) response = fetch(event.request);
return response;
})
);
});
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
);
});
@trishulgoel
caches.match('/data.json')
.then(response => {
updatePage();
fetch('/data.json').
then(result => {
updatePage();
})
})
.catch(() => {
fetch('/data.json').
then(result => {
updatePage();
})
});
In page
@trishulgoel
self.addEventListener('fetch', function(event) {
event.respondWith(
// Try the cache
caches.match(event.request).then(function(response) {
if (response) {
return response;
}
return fetch(event.request).then(function(response) {
if (response.status === 404) {
return caches.match('/404.html');
}
return response
});
}).catch(function() {
return caches.match('/offline.html');
})
);
});
@trishulgoel
- https://serviceworke.rs
- https://developer.mozilla.org
- https://developers.google.com/
@trishulgoel
@trishulgoel
@trishulgoel