Service Worker Internals: (@saket - KMR) & (@piyuesh23)
Service Worker Internals: (@saket - KMR) & (@piyuesh23)
Service Worker Internals: (@saket - KMR) & (@piyuesh23)
@piyuesh23 @saki007ster
Technical Architect, QED42 UI/UX Lead, QED42
Agenda
● Mobile Web Vs Mobile Apps
● Demo
Mobile
Web V
S
Mobile
Apps
67% 33%
?
Tabbed Experience
Offline Enabled
Notifications
Simple
Notifications
Push
Messages
Branding
And Many More...
The Web is Dead
● 08/2010
● http://www.wired.
com/2010/08/ff_webrip/
The Web is Not Dead
● 02/2014
● http://www.wired.
com/insights/2014/02/web-
dead/
Progressive Web Apps
● Fast Loading
● Installable
● Push notifications
Programmable
cache
Offline Web
Geo-fencing
Functional in
2G
Background
case of slow
processing
connections
EDGE
Push
Notifications
https-only
Pillars of SW
Working With
Service Workers
No service worker
Installing
Activate Error
Idle
App-Shell '/script.js',
'/style.css'
];
self.addEventListener('install', function(event) {
event.waitUntil(
Wait before activation until cache.open(cacheName).then(function(cache) {
app-shell is cached. return cache.addAll(staticResources);
});
);
})
self.addEventListener('fetch', function(event) {
navigator checks for the event.respondWith(serverFromCache(event.request));
network. returns true or });
false.
function serveFromCache(request) {
respondWith() receives caches.match(request).then(function(response)) {
only a valid response
return response;
object.
}).catch(function(err) {
return caches.match('/offline.html');
})
}
function serverFromNetwork(event) {
Fetch response from network &
update cache caches.open(cacheName).then(function(cache) {
return fetch(event.request.url).then(function (response) {
cache.put(event.request.url, response.clone());
Response object is a readable return response;
stream object & can be }).catch(function(err) {
consumed only once(either to return cache.match(event.request.url).then(function
update cache or respond back). (response) {
return response;
});
If something goes wrong with
the network fetch, respond back });
with a stale copy from cache. })
}
Got another custom scenario/Handling cache
busting...
Break down Css & Js assets var jsBucket = 'JS';
into different cache buckets. var cssBucket = 'CSS';
var cssResources = [
'/slides.css',
'/styles.css'
];
var jsResources = [
'/slides.js',
'/scripts.js'
];
self.addEventListener('install', function(event) {
Avoid overhead for sw.js check console.log('[install] Kicking off server worker registration.');
on each page using http cache- event.waitUntil(
control headers.
caches.open(cssBucket).then(function(cache) {
cache.addAll(cssResources);
});
caches.open(jsBucket).then(function(cache) {
cache.addAll(jsResources);
});
);
});
Activate fired after installing the self.addEventListener('activate', function(event) {
service worker and all the var cacheWhiteList = [jsBucket, cssBucket];
current versions of sw are
closed. event.waitUntil(function(caches) {
return Promise.all(
caches.map(function(cache) {
if (cacheWhileList.indexOf(cache) == -1) {
return caches.delete(cache);
Use event.replace() inside the }
install event for the installing
})
service worker to take effect
instantly. );
})
})
Browser Support
- global.toolbox.options.debug = true
●
●
●
So How Was It? - Tell Us What You Think
Thanks!