Gareth Jones - @nomiddlename
The average load time for mobile sites is 19 seconds over 3G.
53% of mobile site visits are abandoned if pages take longer than 3 seconds to load.Doubleclick Report
Home screen
Push notifications
Every step between your users and installing your app loses you 20% of them.
// this is in your main javascript
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(() => {
console.log("Service worker registered!");
});
}
//in your service worker - sw.js
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll(filesToCache);
})
)
});
var cacheName = 'pow-v30';
var filesToCache = [
"bower_components/jquery/dist/jquery.min.js",
//other css, etc.
...
"js/index.js",
"data/speakers.json",
"speakers.html",
...
];
//in your service-worker - sw.js
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(keys => {
// delete any old caches
...
})
)
});
//in your service worker - sw.js
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.open(cacheName).then(cache => {
return cache.match(e.request).then(response => {
return response || fetch(e.request);
});
})
)
});
{
"short_name": "POW! 2016",
"start_url": "index.html",
"display": "standalone",
"icons": [{
"src": "images/logo.png",
"sizes": "192x192",
"type": "image/png"
}]
}
//in your main javascript
navigator.serviceWorker.ready.then(reg => {
Notification.requestPermission().then(() => {
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(sub => {
// PushSubscription
})
})
})
//server-side code
const push = require('web-push');
push.sendNotification(
'https://updates.push.services.mozilla....',
{ userPublicKey: '...',
userAuth: '...',
payload: '...' }
).then(() => {
console.log('Message sent.');
});
//meanwhile, back in our service worker
self.addEventListener('push', function(e) {
e.waitUntil(
self.registration.showNotification(
"Schedule change",
{ body: e.body.json().message, icon: '...' }
)
)
});