PWA Tutorial for Beginners #12 - Fetch Events
Skills:
HTML & CSS80%
Key Takeaways
Listens to fetch events in a Progressive Web App using service workers
Full Transcript
okay then so far we've seen these two different events from a service worker install and activate but there's another event that a service worker can listen to fetch events now fetch events occur whenever we try to get something from a server now that could be for example a CSS file like this or a JSON file or JavaScript file these are all fetch requests and it could be an HTML page like these or an image or even from a JavaScript file if we were to make some kind of HTTP request that would be a fetch request and all these with fire fetch events that we can listen to inside our serviceworker and then do something with them okay there so we have an indexed or HTML file running in the browser which was initially fetched from a server in our case a local development server at the moment and normally when the app makes a request for a resource like a stylesheet or images or some other page it sends a request from the browser to the server gets the resource and then that resource is sent back to us in a server response to the browser so that then we can use it in the app now we also have a serviceworker sitting right here in the background potentially listening for fetch requests from our app when they happen now this service work essentially acts as a proxy between our app and the server now when our app makes a request for example for an image or CSS file or something else it goes through our serviceworker like this and at this point a fetch event is emitted and the serviceworker can listen for and react to that event now at that point we could intercept the fetch request we could do nothing and later on as normal to the server and back or we could modify the request before it carries on or even stop the request and return a custom response to the app but why would we do all this why intercept the request at all well eventually our serviceworker will be handling cached assets and at some point when a fetch request is made from the browser we might want to stop that request in its tracks so that we get whatever it was requesting from the cache instead so doesn't have to go to the server and back and then return it to the browser right here and this makes for a really quick experience and good offline behavior and this is at the heart of what a serviceworker can do so we'll talk about cached assets a little bit more later on for now let's try it and listen to a fetch event in our serviceworker okay there so in our serviceworker file we want to now add an event listener to the serviceworker to listen for fetch events so i'm going to say self to add event listener like so and inside we want to now listen for the fetch events now we want to fire a callback function when this happens and that callback function takes as a parameter the events objects so information about that fetch request so what I'm going to do in here is console.log fetch events so we know that's occurred and then comma EVT the event object that we get back right here every time there is some kind of fetch events so now let's save this and because we're saving it and changing the code what's going to happen is it's going to reinstall the serviceworker and this is going to fire but it's not automatically going to activate remember it becomes a serviceworker in waiting so let's just go to the browser after saving this we can see over here that we now have all these fetch events and it did activate automatically in my case because if I go to application I've got this checked updates on reload so when I save and it reloads over here it's updating the serviceworker on reload if you don't have that checked you should have to click skip waiting down here or something like that then we Frisch the page ok anyway now we can see all of these different fetch events logged to the console loads of them so that's for every kind of resource our app is trying to get so the CSS files the images or some kind of HTML file and all of those are logged here now if we go to the network it matches up to all of these different requests right here so this should be the same amount really now if we go back to the console and just expand one of these a random one and we go down we can see this request object right here this is information about the request so we can see that this one is a get request to get this thing right here up is so this request ran through the serviceworker and we listened to that fetch request and we can get information about that request likewise all of the other ones as well if we expand this and come down to request we can see this material icons and if we do another one we can come down here this is manifest Jason so all of these different requests for different things now are being heard by our service worker this one styles dot CSS now later on we're going to use this ability of listening to all these different fetch requests when they're being made to good use when we start using caching with the Service Worker on our app but for now I just wanted to show you how to set up an event listener to listen for these fetch events when they happen
Original Description
Hey gang, in this PWA tutorial we'll see how to listen to fetch requests made by the app.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/pwa-tutorial
+ Modern JavaScript Tutorial - https://www.thenetninja.co.uk/udemy/modern-javascript
🐱💻 🐱💻 Other Related Courses:
+ Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB
Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: HTML & CSS
View skill →Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI