Re: [ServiceWorker] Fetch API respondWith's implicit RETURN (#844)

There's a few things wrong with your code that will cause it to fail or behave unreliably:

```js
self.addEventListener('activate', function(event) {
  self.addEventListener('fetch', optimusConor);
  console.log('Activated', event);
});
```

You shouldn't add event listeners inside other event listeners. Once the service worker is terminated (which happens after a period of not being used, or when the browser shuts down) your fetch listener is gone, because next time the SW runs the activate event isn't fired.

Also, if you want to intercept a fetch request, you must call `event.respondWith` within the event callback.

Check out the [offline cookbook](https://jakearchibald.com/2014/offline-cookbook/), there's also a [Udacity course](https://www.udacity.com/course/offline-web-applications--ud899) which guides you through taking an online-only web app to full offline-first.

This is also an excellent article https://ponyfoo.com/articles/serviceworker-revolution

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/844#issuecomment-195366048

Received on Friday, 11 March 2016 13:43:01 UTC