Skip to content

Instantly share code, notes, and snippets.

@beebole
beebole / sw4.js
Created July 25, 2017 19:53
Mobile offline - SW file
.then( function( ){
return self.clients.claim( );
})
.catch( function( e ){
console.log("Error handling cache", e);
})
);
@beebole
beebole / sw3.js
Created July 25, 2017 19:52
Mobile offline - SW file
self.addEventListener( 'activate', function( event ){
event.waitUntil(
caches.open( 'name_your_cache_container' )
.then( function( cache ) {
return cache.addAll( urlsToCache );
})
@beebole
beebole / sw2.js
Created July 25, 2017 19:52
Mobile offline - SW file
self.addEventListener( 'install', function(){
return self.skipWaiting( );
});
@beebole
beebole / sw10.js
Created July 25, 2017 19:51
Mobile offline - SW file
return caches.open( 'name_your_cache_container' )
.then( function( cache ) {
cache.put( event.request, resp.clone( ));
return resp;
});
});
@beebole
beebole / sw1.js
Created July 25, 2017 19:49
Mobile offline - SW file
var urlsToCache = [ 'index.html',
'.',
'img/bunny1.jpg',
'img/bunny2.jpg',
'img/bunny3.jpg' ];
@beebole
beebole / index(html)2.js
Last active July 25, 2017 19:57
Mobile Offline - Service Worker Initialization
//Listen for claiming of our ServiceWorker
navigator.serviceWorker.addEventListener('controllerchange', function() {
console.log( 'Service worker status changed: ', this.controller.state );
// Listen for changes in the state of our ServiceWorker
navigator.serviceWorker.controller.addEventListener('statechange', function() {
// If the ServiceWorker becomes "activated", let the user know they can go offline!
if (this.state === 'activated') {
window.location.reload( true );
}
});
@beebole
beebole / index(html)1.js
Last active July 25, 2017 19:56
Mobile offline
if ( 'serviceWorker' in navigator ) {
navigator.serviceWorker.register('sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://pure.github.io/pure/libs/pure.js"></script>
</head>
<body>
<div class="testDiv">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://pure.github.io/pure/libs/pure.js"></script>
</head>
<body>
<div class="test">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://pure.github.io/pure/libs/pure.js"></script>
</head>
<body>
<div class="test">