Lab: Offline Status
From: Offline Recipes for Service Workers
Instructions: After this page loads, disconnect from the Internet, then click the Show Another Random Logo button. The image below the buttons should change to one of six random logos. That is because, after the page loaded, a Service Worker downloaded and cached all the assets that this page needs. These assets can now be used offline, and to help the page load faster even when online!
You are now safe to go offline!
Notes: I found two inconsistencies in the original tutorial that gave me bugs to find and correct. Both appear in the service-worker.js
file:
-
When creating the
REQUIRED_FILES
variable, you can just use relative URLs like'app.js'
, but if you just use'/'
, it will cache the absolute root document, not the relative root. For example, for this demo, it cachedhttps://aarontgrogg.com/
, nothttps://aarontgrogg.com/lab/service-workers/offline-status/
, which is what I wanted. So I used the full relative URL for each of my assets, like'/lab/service-workers/offline-status/'
. -
In the
fetch
listener, thecaches.match
receives theevent.request
, but what it really needs is theevent.request.url
.