Working with Live Data in the Service Worker
Key Takeaways
Uses Cache API and IndexedDB to store dynamic resources in a service worker
Full Transcript
[Music] this is an example of an e-commerce site with multiple kinds of data in this case there are images and numeric data as well as HTML and CSS resources and probably JavaScript as well the question here is which data should go where the general rule is that resources addressed through a URL will be stored in the cache dynamic resources will be stored in an indexed DB database for example app shell resources images and style sheets should be stored in the cache dynamic JSON data should be stored in index DB start by storing data locally here we're using the cache API and the index DB API also known as the ID V let's look at storing JSON data with ID B so what does ID b look like it does not look like a conventional SQL table ID B data is sort of key value pairs in object stores a single ID B database can have multiple object stores this could be a clothing object store inside a product database now the activate event is a good place to create an ID B database point one you don't want to create it more often than needed for efficiency sake and point two doing that during installation could cause issues with the existing service worker note that event dot wait until ensures that the service worker does not terminate preemptively during async actions here we create an ID B products database it is version one inside the products database we create a closing object store this will hold all of the clothing objects the clothing object store has a key path of ID now this means that the objects in this store will be organized and accessed by the ID property of the clothing objects note that we're using Jake Archibald's indexed DB promised library to enable promise syntax we IDB so let's look at storing resources with the cache API now a common pattern is to cash assets on service worker installation note that event or wait until ensures that the service worker does not terminate preemptively during async actions like we said in this example we create a cache v1 cache and store stack assets that's HTML CSS JavaScript images and so on with the cache API now we can get data from ID B instead of the network here we open the products database and create a new transaction on the clothing store of read-only type we don't need to write data we can then access the store and retrieve all of the items these items can then be used to update the UI or whatever is needed are just a word on transactions these are a wrapper around an operation or group of operations to ensure database integrity if one of the actions within a transaction fails none of them are applied and the database returns to the state it was in before the transaction began all read or write operations in index DB must be part of a transaction this allows for atomic read-modify-write operations without worrying about other threads acting on the database at the same time now we can also get resources like HTML CSS JavaScript and images from the cache instead of the network here we add a fetch listener to the serviceworker when a fetch is made for a resource we try to find the resource in the cache and return it however if it isn't in the cache we still want to go to the network for it there are plenty of resources to help you get started for links take a look at the materials that accompany this video but first one more thing what can we do about actions that a user takes when they're offline for example purchasing a product we can record them in ITB it's possible to record actions a user takes while offline in this example the user is trying to make an item purchase via HTTP request which will fail if offline if the purchase fails the catch block will execute this is where we can store the item or user action in ITV how do we use this once connectivity returns well once connectivity returns the recorded items or actions in a DB can be retrieved and sent if the requests are successful the corresponding item action can be deleted from the IDB records
Original Description
Most modern web apps rely on a combination of static and dynamic resources. This video describes how to use the Cache API and IndexedDB together in your app to store multiple types of data.
Codelabs, slides and more information are available from the Progressive Web Apps Training home page: https://developers.google.com/web/ilt/pwa
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Chrome for Developers · Chrome for Developers · 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
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
Web Directions Code 2015 round up
Chrome for Developers
Maintainable Code - HTTP203
Chrome for Developers
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
The Guardian - Supercharged
Chrome for Developers
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
The Future of JavaScript - HTTP203
Chrome for Developers
Data Binding 101 -- Polycasts #28
Chrome for Developers
The Guardian part 2 - Supercharged
Chrome for Developers
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
Binding to Objects -- Polycasts #30
Chrome for Developers
Player FM - Supercharged
Chrome for Developers
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
Jake Beats Wikipedia - HTTP203
Chrome for Developers
Supercharged Observers! -- Polycasts #32
Chrome for Developers
Jai's Web blog - Supercharged
Chrome for Developers
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
Google+ Performance Improvement Comparison
Chrome for Developers
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
Binding to Arrays -- Polycasts #35
Chrome for Developers
HTTP2 - HTTP203
Chrome for Developers
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
Call For Submissions - Supercharged
Chrome for Developers
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
Highlights from Chrome Dev Summit 2015
Chrome for Developers
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Medium · Programming
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Medium · DevOps
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
🎓
Tutor Explanation
DeepCamp AI