Skip to content

Commit

Permalink
Add service workers scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
poctek committed Jan 2, 2017
1 parent 0d8b2b6 commit d60dc8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cacher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const CACHE_NAME = 'blog'
const URLS_TO_CACHE = [
'/',
'/index.html',
'/assets/css/main.css',
'/assets/js/script.js',
'/assets/header-background.jpeg',
'/assets/favicon-96x96.png'
]

this.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(URLS_TO_CACHE)
})
)
})

this.addEventListener('fetch', event => {
let request = event.request
let url = new URL(event.request.url)

if (url.origin !== location.origin)
return

event.respondWith(
caches.open(CACHE_NAME).then(cache => {
return cache.match(request).then(result => {
if (result) {
return result
} else {
return fetch(request).then(response => {
cache.put(request, response.clone())
return response
})
}
})
})
)
})
9 changes: 9 additions & 0 deletions service-workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/cacher.js', {scope: '/'})
.then(response => {
console.log('Worker installed successfully. Scope: ', response.scope)
})
.catch(error => {
console.log("There's an error in the worker installation: ", error)
})
}

0 comments on commit d60dc8a

Please sign in to comment.