-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update offline service worker to... work. The new approach always tri…
…es online first, and falls back to cache. Only specific files are ever cached which solves a pollution problem.
- Loading branch information
Showing
4 changed files
with
49 additions
and
68 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
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
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,45 @@ | ||
'use strict'; | ||
// Simple offline service worker. | ||
// | ||
// On the main page include: | ||
// if ('serviceWorker' in navigator) | ||
// navigator.serviceWorker.register('simple-offline-service-worker.js'); | ||
// | ||
// This cache always requests from the network to keep the cache fresh. There is a tradeoff here | ||
// because we'll wait ages for a slow network despite having a cached response ready to go. | ||
|
||
var version = 'v1.0.2'; | ||
|
||
// FIXME: Is it possible to serve from ./ and also have an offline app using manifest.json without | ||
// making two requests? | ||
var offlineFiles = [ './', './index.html' ]; | ||
|
||
self.addEventListener('install', function(event) { | ||
// Cache our list of files. | ||
event.waitUntil(caches.open(version).then(function(cache) {cache.addAll(offlineFiles); })); | ||
}); | ||
|
||
self.addEventListener('activate', function(event) { | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
self.addEventListener('fetch', function(event) { | ||
event.respondWith( | ||
fetch(event.request).then(function(networkReponse) { | ||
// Check if this request is already in our cache. We only want to cache previously | ||
// cached items to prevent the cache from getting polluted. | ||
caches.open(version).then(function(cache) { | ||
cache.match(event.request).then(function(previouslyCachedResponse) { | ||
if (!previouslyCachedResponse) | ||
return; | ||
// Clone the response since we're also returning it below. | ||
cache.put(event.request, networkReponse.clone()); | ||
}); | ||
}); | ||
return networkReponse; | ||
}).catch(function(networkIssue) { | ||
// Return from the cache if available, or explode which will 404. | ||
return caches.match(event.request); | ||
}) | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.