Let's take a look at some uses and examples of service workers. This is where I think things really start to stick. So a really low hanging fruit here, the example we actually just looked at, is showing critical information when a site goes offline. So if someone loses connectivity entirely, you can still give them a usable experience. And this is really particularly useful for things like restaurants, conferences, and hotels. With a restaurant, for example, you might let the user know they're offline and then give them other things that they might need or want from your restaurant. Phone number, an address, or directions on how to get there. Maybe an abridged menu and a phone number to call to make reservations.
For a conference, you might have the schedule for that conference, the venue, and the name of the organizer in case someone needs to get in touch with someone and just can't get the webpage to pull up or they're having connection issues, which is pretty common in hotels and conference venues where a lot of people are using the Wi-Fi and things kind of break or go down. Extending this a little bit further, you can cache pages in real time as people visit them, and then if for some reason they go offline, you can show them a list of the pages they visited and make those available to them even though the site is offline. And this is particularly useful for reference sites, news sites, social networks, utility apps, just because the site is offline doesn't mean people can't still use it and access things that they've already been to.
You can use Service Workers to cache core assets for performance reasons. Things like CSS, JavaScript, images, fonts, any of these really heavy commonly used files, you can save them locally and then serve them from the network, from the cache rather, instead of going out to the network. This is also really great for people who are on low data plans or live in an area where downloading things takes a really long time. This can dramatically speed up performance. Once you have that asset saved, you can serve it from your cache over and over again and it returns instantaneously instead of taking a few hundred milliseconds or several seconds, depending on what a person's connection speeds are and as you may have picked up by now, you can use these different techniques in conjunction. So you don't have to only choose offline first or network first. You can use those different approaches for different types of assets. So for things like CSS and JavaScript, you may go offline first for those and then you may use network first for your HTML and API requests and have those as a fallback when things go offline.
Now, on the more extreme end of things, you can go fully offline and this is great for things like apps and games where once you download the initial assets, the HTML, the CSS and the JavaScript, you have everything you need and you never need to go back out to the network. So think about a utility app like a calculator or a game like a JavaScript-based Pac-Man game. And if you pair this with a manifest.json file, you can turn the simple web app with a service worker into a progressive web app that users can install onto their home screen and then load fully offline without any of the browser Chrome so it functions like a native app, but it's a web app that works completely offline. Now my personal favorite use for service workers is replacing single page apps, which tend to be really fragile and easily broken with multi-page apps that are more resilient and in my experience, actually provide a better developer experience. With a single page app, the whole site or app exists in a single HTML file. JavaScript renders the content, handles URL routing and so on. Now the thinking behind this is that because only the content refreshes and you don't have to re-download all of the JavaScript and CSS, each page loads faster and if you have an API driven site where you're getting data from an API request and then using that to render your content, you can hold that in memory as the pages change and you don't have to constantly make new API calls every time the page reloads, but as we've already learned, service workers can give you a lot of those same benefits and the problem with single page apps is that they break a bunch of stuff that the browser just gives you for free out of the box and you need to go recreate it with JavaScript. For example, you need to intercept clicks on links and suppress them. You also want to detect if the user right clicked on the link or command clicked and they're trying to open it in a new tab or window and allow them to do that. A lot of single page apps break this experience and break user expectations. Once you detect that click, you need to figure out which HTML to show based on the URL path, then you need to update the URL in the address bar without triggering a page reload because that would defeat the entire purpose.
Comments