refactor(sw): check for fallbackUrl
This commit is contained in:
parent
f80807eebd
commit
343568042c
1 changed files with 7 additions and 8 deletions
|
@ -78,9 +78,6 @@ const networkFirst = async ({ request, fallbackUrl }) => {
|
|||
try {
|
||||
// Try to get the resource from the network for 5 seconds
|
||||
const responseFromNetwork = await fetch(request.clone())
|
||||
// response may be used only once
|
||||
// we need to save clone to put one copy in cache
|
||||
// and serve second one
|
||||
putInCache(request, responseFromNetwork.clone())
|
||||
console.info('using network response', responseFromNetwork.url)
|
||||
return responseFromNetwork
|
||||
|
@ -93,12 +90,14 @@ const networkFirst = async ({ request, fallbackUrl }) => {
|
|||
return responseFromCache
|
||||
}
|
||||
|
||||
// Try the fallback
|
||||
// If fallback is provided, try to use it, otherwise return error
|
||||
if (fallbackUrl) {
|
||||
const fallbackResponse = await cache.match(fallbackUrl)
|
||||
if (fallbackResponse) {
|
||||
console.info('using fallback cached response...', fallbackResponse.url)
|
||||
return fallbackResponse
|
||||
}
|
||||
}
|
||||
|
||||
// when even the fallback response is not available,
|
||||
// there is nothing we can do, but we must always
|
||||
|
|
Loading…
Reference in a new issue