You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
By following your lab I have noticed the registration of the route for articles that uses the NetworkFirst is developed wrong for the latest version of Workbox libraries.
In first place we need to use the new operator for NetworkFirst as a construct and not for networkFirst as a property.
Then we handle the response for both the case when there is a response but the page is unknown (404) and when there is no response from the server because we are offline. In the latter we need to handle this not within the Promise.prototype.then() method but within the Promise.prototype.catch() method.
And in both cases we need to use workbox.precaching.getCacheKeyForURL() method in order to get the correct key for caches.match().
The code looks like this:
constarticleHandler=newworkbox.strategies.NetworkFirst({cacheName: 'articles-cache',plugins: [newworkbox.expiration.Plugin({maxEntries: 50})]});workbox.routing.registerRoute(/(.*)article(.*)\.html/,args=>{returnarticleHandler.handle(args).then(response=>{if(response.status===404){constpageNotFoundKey=workbox.precaching.getCacheKeyForURL('pages/404.html');returncaches.match(pageNotFoundKey);}returnresponse;}).catch(error=>{// The strategy could not generate a response for this URL.constpageOfflineKey=workbox.precaching.getCacheKeyForURL('pages/offline.html');returncaches.match(pageOfflineKey);});});
Please check it out.
The text was updated successfully, but these errors were encountered:
Hi,
By following your lab I have noticed the registration of the route for articles that uses the
NetworkFirst
is developed wrong for the latest version of Workbox libraries.In first place we need to use the
new
operator forNetworkFirst
as a construct and not fornetworkFirst
as a property.Then we handle the response for both the case when there is a response but the page is unknown (404) and when there is no response from the server because we are offline. In the latter we need to handle this not within the
Promise.prototype.then()
method but within thePromise.prototype.catch()
method.And in both cases we need to use
workbox.precaching.getCacheKeyForURL()
method in order to get the correct key forcaches.match()
.The code looks like this:
Please check it out.
The text was updated successfully, but these errors were encountered: