Extending Workbox: Custom strategies and plugins

Chrome for Developers · Intermediate ·📰 AI News & Updates ·5y ago

Key Takeaways

The video discusses extending Workbox, a set of libraries for common service worker use cases, by writing custom strategies and plugins, allowing developers to handle unique caching scenarios beyond the common ones provided by Workbox.

Full Transcript

hi folks we're going to take a quick tour of some ways of extending workbox by the end you'll be writing your own strategies and plugins and hopefully sharing them with the world but first what's workbox if you've never heard of it before you could follow that link to the docs and then come back to this video at its core workbox is a set of libraries to help with common service worker caching scenarios and when we've talked about workbox in the past the emphasis has been on common scenarios for most developers the strategies that workbox already provides will handle your caching needs workbox includes ready-to-use strategies like still while revalidate where a cached response is used to respond to a request immediately while the cache is also updated so that it's fresh the next time around other common strategies like network first falling back to the cache are available for use as well but what if you wanted to go beyond these common scenarios what if your use case involves more arrows i've got you covered let's go beyond the common caching scenarios and talk about customizing workbox to help with all your caching needs first let's cover writing your own custom caching strategies workbox version 6 offers a new strategy based class that sits in front of lower level apis like fetch and cache storage you can extend the strategy base class and then implement your own logic in the underscore handle method let's go back to that diagram with all the arrows this represents a strategy that can handle multiple simultaneous requests for the same url by deduplicating them a copy of the response is then used to fulfill all the in-flight requests saving bandwidth that would otherwise be wasted and here's all the code you need to implement that custom strategy just a quick note this code assumes that all requests for the same url can be satisfied with the same response which won't always be the case if cookies or session state information comes into play i'll link to this code later but there are a few things i wanted to call attention to this class extends an existing strategy network first and adds in some additional state mapping the in-flight requests to their corresponding response promises it overrides the underscore handle method checking to see if there's already an inflight request for the same url if there is the strategy will wait until there's a response for that earlier request but if there isn't already an inflate request for the same url our strategy just calls the underscore handle method on its parent class to get a response and adds to the mapping of inflight requests and cleans up after itself once the response is received and that's all the code you need here's another example of a custom strategy this is a twist on still while revalidate where both the network and cache are checked at the same time with a race to see which will return a response first let's take a quick walk through code that implements that strategy like before our class extends a base in this case we're extending a generic strategy class that workbox provides this is a good starting point when you need more control over the sequence of network and cache lookups all of our response generation logic is in the underscore handle method which has passed two parameters the browser's request and then a handler parameter which is an instance of the strategy handler class although it's not required it's strongly recommended that you use the handler parameter to make network requests and interact with the cache like we're doing here those handler methods will automatically pick up the cache name you've configured for the strategy as well as invoke the plug-in lifecycle callbacks that we'll talk about in a bit a strategy handler instance provides four helper methods there are fetch and cache put along with the two others we saw in the previous slide writing a workbox strategy class is a great way to package up response logic in a reusable and shareable form you can drop any of these strategies directly into your existing workbox routing rules and a properly written strategy will automatically work with all workbox plugins as well this applies to the standard plugins that workbox provides like the one that handles cache expiration but it also applies to plugins that you write yourself because another great way to extend workbox is to write your own plugins so what is a workbox plugin and why would you write your own let's go back to the diagram we looked at before adding in a plug-in doesn't fundamentally change the flow of this diagram but it allows you to add an extra code that will be run at critical points in the lifetime of a request like when a network request fails or when a cache response is about to be returned to the page each plugin responds to one or more lifecycle events which are invoked when a strategy handler interacts with a network or cache you can see a list of a few of the existing lifecycle events here we'll link to a webpage for this information later on as well workbox version 6 has a number of additional lifecycle events that plugins can react to all corresponding to different stages in a strategy's lifecycle let's combine a couple of those lifecycle callbacks into a reusable plugin that provides a fallback whenever a strategy would otherwise generate an error response this class implements two lifecycle callbacks fetch did succeed and handler did error it can be added to any strategy class and if running that strategy does not result in a 200 okay response he'll use a backup response from the cache instead my colleague andre talks more about using this plugin within a trusted web activity in his chrome dev summit talk and you can watch for that for more details now that you know more about custom strategies and plugins you might be wondering which one to write for a given use case i think a good rule of thumb is to sketch out a diagram of your desired request and response flow if your diagram has a novel set of connections like all these extra arrows then that's a sign that a custom strategy is the best solution conversely if your diagram ends up looking mostly like a standard strategy but with a few extra pieces of logic injected at key points then you should probably write a custom plugin whichever approach to customizing workbox you go with i hope this talk has inspired you to do the following write your own strategies and plugins and then release them on npm tagged with workbox strategy or workbox dash plugin or just share them with the rest of your organization can learn more including a detailed look at all the sample code and events by visiting web.dev slash extending dash workbox thanks to everyone for tuning in now go out there and extend workbox and share what you build

Original Description

Workbox is a set of libraries for common service worker use cases. But what if your service worker use case is . . . a little less common? Workbox can still help! This talk dives into custom response strategies, and explains how plugins can hook into crucial events in the lifecycle of any strategy. Resources: Extending Workbox → https://goo.gle/3743mdU Workbox → https://goo.gle/workbox Speaker: Jeff Posnick Subscribe to Google Chrome Developers here → https://goo.gle/ChromeDevs Watch all Chrome Developer Summit sessions here → https://goo.gle/cds20-sessions #chromedevsummit #chrome #workbox event: Chrome Dev Summit 2020; re_ty: Publish; product: Chrome - Web - Workbox; fullname: Jeff Posnick;
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 Polymer Performance Patterns (The Polymer Summit 2015)
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
2 Polymer Power Tools (The Polymer Summit 2015)
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
3 Chrome Dev Summit 2014 – Chrome Case Studies
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
4 Web Directions Code 2015 round up
Web Directions Code 2015 round up
Chrome for Developers
5 Maintainable Code - HTTP203
Maintainable Code - HTTP203
Chrome for Developers
6 iron-ajax… wat?! -- Polycasts #26
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
7 The Guardian - Supercharged
The Guardian - Supercharged
Chrome for Developers
8 ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
9 #AskPolymer: Rob answers all the questions ever -- Polycasts #27
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
10 The Future of JavaScript - HTTP203
The Future of JavaScript - HTTP203
Chrome for Developers
11 Data Binding 101 -- Polycasts #28
Data Binding 101 -- Polycasts #28
Chrome for Developers
12 The Guardian part 2 - Supercharged
The Guardian part 2 - Supercharged
Chrome for Developers
13 The Future of Web Audio: with Chris Wilson and Chris Lowis
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
14 Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
15 Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
16 #AskPolymer: How do you make the show? -- Polycasts #29
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
17 Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
18 Binding to Objects -- Polycasts #30
Binding to Objects -- Polycasts #30
Chrome for Developers
19 Player FM - Supercharged
Player FM - Supercharged
Chrome for Developers
20 Where’s the Designer? #AskPolymer -- Polycasts #31
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
21 Jake Beats Wikipedia - HTTP203
Jake Beats Wikipedia - HTTP203
Chrome for Developers
22 Supercharged Observers! -- Polycasts #32
Supercharged Observers! -- Polycasts #32
Chrome for Developers
23 Jai's Web blog - Supercharged
Jai's Web blog - Supercharged
Chrome for Developers
24 Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
25 What about internationalization? #AskPolymer -- Polycasts #33
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
26 Developing for Billions (Chrome Dev Summit 2015)
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
27 Google+ Performance Improvement Comparison
Google+ Performance Improvement Comparison
Chrome for Developers
28 Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
29 Progressive Web Apps (Chrome Dev Summit 2015)
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
30 Instant Loading with Service Workers (Chrome Dev Summit 2015)
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
31 Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
32 Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
33 Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
34 Polymer - State of the Union (Chrome Dev Summit 2015)
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
35 Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
36 Introduction to RAIL (Chrome Dev Summit 2015)
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
37 DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
38 RAIL in the real world (Chrome Dev Summit 2015)
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
39 #ChromeDevSummit talks are up - W00T! -- Polycast #34
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
40 V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
41 Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
42 Owning your performance: RAIL (Chrome Dev Summit 2015)
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
43 HTTP/2 101 (Chrome Dev Summit 2015)
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
44 Leadership Panel (Chrome Dev Summit 2015)
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
45 Build Processes, Totally Tooling Tips (S2, Ep 5)
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
46 Accessibility (Chrome Dev Summit 2015)
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
47 Binding to Arrays -- Polycasts #35
Binding to Arrays -- Polycasts #35
Chrome for Developers
48 HTTP2 - HTTP203
HTTP2 - HTTP203
Chrome for Developers
49 Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
50 Call For Submissions - Supercharged
Call For Submissions - Supercharged
Chrome for Developers
51 Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
52 Testing AJAX with Web Component Tester -- Polycasts #37
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
53 Slack: Extended Xmas Special - Supercharged
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
54 Browser testing with Travis & Sauce Labs -- Polycasts #38
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
55 Optimize for production with Vulcanize -- Polycasts #39
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
56 Highlights from Chrome Dev Summit 2015
Highlights from Chrome Dev Summit 2015
Chrome for Developers
57 Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
58 Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
59 How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
60 Colors – DevTools Tonight #0 (Pilot)
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers

This video teaches developers how to extend Workbox by writing custom strategies and plugins to handle unique caching scenarios, and how to integrate these customizations with the Workbox API.

Key Takeaways
  1. Write a custom strategy by extending the StrategyBase class
  2. Implement the _handle method to define the caching logic
  3. Create a plugin by implementing lifecycle callbacks
  4. Add the plugin to a strategy class
  5. Test and deploy the custom strategy or plugin
💡 Custom strategies and plugins can be used to extend Workbox and handle unique caching scenarios, and can be shared with the community by releasing them on npm.

Related Reads

Up next
Power Automate Excel Tutorial: Add, Update and Read from Excel
Thomas Janssen
Watch →