-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
}) | ||
}) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |