Coding task · JavaScript and React course · Module 9: Modern React Hooks

Practice: Service Worker

The Cache API has a match(request) method, which returns the saved response or undefined, and a put(request, response) method, which saves it. The cache gets response.clone(), because the body of a response can be read only once.

Task

The cacheFirst, networkFirst and pickStrategy functions go into the sw.js file and handle the fetch event in the Service Worker. Static files should use the cache-first strategy (a copy from the cache first, the network only when there is no copy), and data from /api/ the network-first strategy (a fresh response from the network first, a copy from the cache only when offline). Fill in the blanks: ___BLANK1___ is the cache method that looks for a saved response, ___BLANK2___ is the cache method that saves a response, and ___BLANK3___ is the strategy function that pickStrategy returns for /api/ addresses. The panel in the preview lets you switch the network off and check where the responses come from.