From the course: JavaScript: Progressive Web Applications

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Intercepting requests using Fetch

Intercepting requests using Fetch - JavaScript Tutorial

From the course: JavaScript: Progressive Web Applications

Start my 1-month free trial

Intercepting requests using Fetch

- [Instructor] The next thing to do is to get the service worker to intercept request the PWA. That way, we can serve up cached files and not worry about a network connection. We'll do this by overriding the service workers fetch method. So let's add an event listener for the fetch events. So that's, self dot add Event Listener. And we'll listen for the fetch event, with our event handler function, which get the event, from the event handler. And I'll put the semicolon at the end because I always forget the punctuation. The fetch event will be called when any request is made on the PWA. Here is where we can respond with cached files. We'll do that with a respond-with method call, on the event object. So before I do that, let's write ourselves a note in the console and we'll console dot log. And maybe a message like, the fetch event occurred. Let's append to that, the request URL for the fetch event. So, event dot request dot URL. So that'll show us the resource that's being fetched by…

Contents