From 1aff011c9c601b87ea339b5661cca4a868102c8d Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Sat, 17 Oct 2015 12:43:24 +0100 Subject: [PATCH 1/9] Method to register SW --- public/js/main/IndexController.js | 5 +++++ public/js/sw/index.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/public/js/main/IndexController.js b/public/js/main/IndexController.js index ff7134b1..3383925e 100644 --- a/public/js/main/IndexController.js +++ b/public/js/main/IndexController.js @@ -8,8 +8,13 @@ export default function IndexController(container) { this._toastsView = new ToastsView(this._container); this._lostConnectionToast = null; this._openSocket(); + this._registerServiceWorker(); } +IndexController.prototype._registerServiceWorker = function() { + // TODO: register service worker +}; + // open a connection to the server for live updates IndexController.prototype._openSocket = function() { var indexController = this; diff --git a/public/js/sw/index.js b/public/js/sw/index.js index e69de29b..8688a064 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -0,0 +1,3 @@ +self.addEventListener('fetch', function(event) { + console.log(event.request); +}); \ No newline at end of file From a90fb6a2949f20ff0572bef55e589a74f4e00d32 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 14 Sep 2015 16:40:58 +0100 Subject: [PATCH 2/9] Register SW and log requests --- public/js/main/IndexController.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/public/js/main/IndexController.js b/public/js/main/IndexController.js index 3383925e..8524fc8e 100644 --- a/public/js/main/IndexController.js +++ b/public/js/main/IndexController.js @@ -12,7 +12,13 @@ export default function IndexController(container) { } IndexController.prototype._registerServiceWorker = function() { - // TODO: register service worker + if (!navigator.serviceWorker) return; + + navigator.serviceWorker.register('/sw.js').then(function() { + console.log('Registration worked!'); + }).catch(function() { + console.log('Registration failed!'); + }); }; // open a connection to the server for live updates From 4f2c24537247891e802cce8275250caf8cca13bd Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Fri, 13 Nov 2015 12:52:00 +0000 Subject: [PATCH 3/9] TODOs for custom response task --- public/js/sw/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index 8688a064..478574a2 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,3 +1,6 @@ self.addEventListener('fetch', function(event) { + // TODO: respond to all requests with an html response + // containing an element with class="a-winner-is-me". + // Ensure the Content-Type of the response is "text/html" console.log(event.request); }); \ No newline at end of file From de6f6c7993bcfa07fe2906e6dcf3788903efd3e0 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Thu, 27 Aug 2015 16:32:05 +0100 Subject: [PATCH 4/9] Custom HTML response --- public/js/sw/index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index 478574a2..d329f3ad 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,6 +1,7 @@ self.addEventListener('fetch', function(event) { - // TODO: respond to all requests with an html response - // containing an element with class="a-winner-is-me". - // Ensure the Content-Type of the response is "text/html" - console.log(event.request); + event.respondWith( + new Response('Hello!', { + headers: {'Content-Type': 'text/html'} + }) + ); }); \ No newline at end of file From adf8827f66860b32d9a5d76ef276c63c70e61d74 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 19 Oct 2015 14:57:43 +0100 Subject: [PATCH 5/9] Responding with gif --- public/js/sw/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index d329f3ad..b5ab6714 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,7 +1,7 @@ self.addEventListener('fetch', function(event) { + // TODO: only respond to requests with a + // url ending in ".jpg" event.respondWith( - new Response('Hello!', { - headers: {'Content-Type': 'text/html'} - }) + fetch('/imgs/dr-evil.gif') ); }); \ No newline at end of file From b593b213a6f4ee3b26d7eda6a22e43ac14872360 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 19 Oct 2015 15:16:08 +0100 Subject: [PATCH 6/9] jpg => gif --- public/js/sw/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index b5ab6714..1d718791 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,7 +1,7 @@ self.addEventListener('fetch', function(event) { - // TODO: only respond to requests with a - // url ending in ".jpg" - event.respondWith( - fetch('/imgs/dr-evil.gif') - ); + if (event.request.url.endsWith('.jpg')) { + event.respondWith( + fetch('/imgs/dr-evil.gif') + ); + } }); \ No newline at end of file From de595542d20e0baf6a8c1420956c23db9b8ee389 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Wed, 21 Oct 2015 13:38:22 +0100 Subject: [PATCH 7/9] Handling 404 and failure --- public/js/sw/index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index 1d718791..15fd2457 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,7 +1,15 @@ self.addEventListener('fetch', function(event) { - if (event.request.url.endsWith('.jpg')) { - event.respondWith( - fetch('/imgs/dr-evil.gif') - ); - } + event.respondWith( + fetch(event.request).then(function(response) { + if (response.status === 404) { + // TODO: instead, respond with the gif at + // /imgs/dr-evil.gif + // using a network request + return new Response("Whoops, not found"); + } + return response; + }).catch(function() { + return new Response("Uh oh, that totally failed!"); + }) + ); }); \ No newline at end of file From c0996c125133b81c3313544db9073bf907772bec Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Wed, 21 Oct 2015 14:15:43 +0100 Subject: [PATCH 8/9] Gif on 404! --- public/js/sw/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index 15fd2457..f53f703f 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -5,7 +5,7 @@ self.addEventListener('fetch', function(event) { // TODO: instead, respond with the gif at // /imgs/dr-evil.gif // using a network request - return new Response("Whoops, not found"); + return fetch('/imgs/dr-evil.gif'); } return response; }).catch(function() { From 1eca55b6efb58b6d4b5537701abcaa4a884dd5cc Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Fri, 23 Oct 2015 10:27:03 +0100 Subject: [PATCH 9/9] Boilerplate for installing --- public/js/sw/index.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/public/js/sw/index.js b/public/js/sw/index.js index f53f703f..6b2c3ab6 100644 --- a/public/js/sw/index.js +++ b/public/js/sw/index.js @@ -1,15 +1,20 @@ -self.addEventListener('fetch', function(event) { - event.respondWith( - fetch(event.request).then(function(response) { - if (response.status === 404) { - // TODO: instead, respond with the gif at - // /imgs/dr-evil.gif - // using a network request - return fetch('/imgs/dr-evil.gif'); - } - return response; - }).catch(function() { - return new Response("Uh oh, that totally failed!"); - }) +self.addEventListener('install', function(event) { + var urlsToCache = [ + '/', + 'js/main.js', + 'css/main.css', + 'imgs/icon.png', + 'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff', + 'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff' + ]; + + event.waitUntil( + // TODO: open a cache named 'wittr-static-v1' + // Add cache the urls from urlsToCache ); +}); + +self.addEventListener('fetch', function(event) { + // Leave this blank for now. + // We'll get to this in the next task. }); \ No newline at end of file