This course has been updated! We now recommend you take the Build Progressive Web Apps (PWAs) from Scratch course.
Transcript from the "Service Worker Demo" Lesson
>> Steve Kinney: The example I just showed you is silly right? Why would you ever do that, right? If you wanted to respond on the page, why wouldn't just respond with that page, right? The interesting part is like, okay, a user made a request, figuring out what exactly we wanna send them is where I think things get a little bit interesting.
[00:00:19] So we're gonna take a look at an example together, this one's called intercepting fetch. And we'll,
>> Steve Kinney: This is firstname.lastname@example.org, and we'll go ahead and,
>> Steve Kinney: Let me biggerize everything for you, that's a technical term, biggerize.
>> Steve Kinney: Cool.
>> Steve Kinney: If we go in here, we take a look at the code, we have a fetch event in here.
[00:01:09] We have some URLs, and these are mostly stored in variables right now cuz they're on Glitch's CDN. But they're very long, and you can assume that one is a URL of a picture of the Beatles. The other one is a URL of a picture of Oasis, like same thing, right?
[00:01:33] And we have this other remote image. And you can see, in this fetch event, we'll console log the request. We'll say, hey, if the event's request URL equals the Beatles, if they're looking for a picture of the Beatles. In that case, we will intercept the request and we'll respond with sending a network request to go get a picture of Oasis instead and giving them that.
[00:02:03] So if we go back over, I do need to clear out this service work. You can see that this is a picture of Oasis.
>> Steve Kinney: If we go to the index@html we are definitely asking for a picture of the Beatles, right?
>> Steve Kinney: We intercept with the request. We said, I know what you asked for, let me go get this other thing from the network and give it to you.
[00:02:28] If we threw in some different picture, if we threw in,
>> Steve Kinney: A picture of,
>> Steve Kinney: I can edit right now, log in later. If we place another image, well, that wouldn't hit our IF statement, and it would go through to the network. But we have the ability to write conditional logic.
[00:03:09] You can take the troll level very high with this and write all this conditional logic to check, but we can basically take any request that comes through. Either let it go through to the network or, b, intercept it with something else. Right now we are making a different network request and we're basically redirecting it and responding with the thing that was redirected to.
[00:03:30] In a little bit, we'll talk about, hey, last time you were online or when we load it, we cached a bunch of things. Well, let's try to do the request, if we can't do the request, here's a cached version, right? We can write this logic that says, we want the most recent image, but if we can't, here's one that we already have, right?
[00:03:51] And a bunch of different things, there's a lot of strategies around this. It's like, yeah, get me a thing, if I can't get a thing, get the one from cache. That is one of the very straightforward use cases, right? At one part we're gonna show you all the different ways.
[00:04:03] Depending on what you're trying to build for your app and depending on which part of your application, you want to use a different strategy and we'll talk about that in a little bit.