by Enrico Zimuel
Senior Software Engineer
Rogue Wave Software, Inc.
WebAppConf 2017, Torino, 16 Novembre
|
sviluppareinphp7.it |
jsbestpractices.it |
phpbestpractices.it |
Migliorare la User Experience degli utenti web:
"53% of mobile site visits are abandoned if pages take longer than 3 seconds to load"Fonte: Double Click, The need for mobile speed
"The average load time for mobile sites is 19 seconds over 3G connections"Fonte: Double Click, The need for mobile speed
"Mobile sites that load in 5 seconds earn up to 2x more mobile and revenue than those whose sites load in 19 seconds"Fonte: Double Click, The need for mobile speed
Click on the image to start the animation
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(reg) {
// Registration was successful
console.log('Registration ok, scope: ', reg.scope);
}, function(err) {
// Registration failed
console.log('Registration error: ', err);
});
});
}
var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
'/',
...
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
console.log('Fetch event for ', event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request);
}).catch(function(error) {
// Error
})
);
});
if ('serviceWorker' in navigator && 'PushManager' in window) {
console.log('Service Worker and Push is supported');
navigator.serviceWorker.register('sw.js')
.then(function(swReg) {
console.log('Service Worker is registered', swReg);
swRegistration = swReg;
})
.catch(function(error) {
console.error('Service Worker Error', error);
});
} else {
console.warn('Push messaging is not supported');
pushButton.textContent = 'Push Not Supported';
}
swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: ''
})
.then(function(subscription) {
console.log('Subscription ok');
})
.catch(function(err) {
console.log('Failed to subscribe: ', err);
});
self.addEventListener('push', function(event) {
console.log('Push Received.');
console.log('Push with data: "${event.data.text()}"');
const title = 'Push notification';
const options = {
body: 'Yay it works.',
icon: 'images/icon.png',
badge: 'images/badge.png'
};
event.waitUntil(self.registration.showNotification(title, options));
});
function hello(string $id, User $user): array
{
$name = $user->getName($id);
return [ 'Hello' => $name ];
}
Email: enrico.zimuel [at] roguewave.com
Seguimi su Twitter: @ezimuel