refactor: tryCache function
This commit is contained in:
parent
9fef0dd913
commit
fb1f1e7cf6
1 changed files with 26 additions and 16 deletions
42
src/sw.js
42
src/sw.js
|
@ -16,8 +16,23 @@ const putInCache = async (request, response) => {
|
||||||
await cache.put(request, response);
|
await cache.put(request, response);
|
||||||
};
|
};
|
||||||
|
|
||||||
const networkFirst = async ({ request, preloadResponsePromise, fallbackUrl }) => {
|
const tryCache = async (request, fallbackUrl) => {
|
||||||
|
// Try get the resource from the cache
|
||||||
|
const responseFromCache = await caches.match(request);
|
||||||
|
if (responseFromCache) {
|
||||||
|
console.info('[cozy-sw]: using cached response', responseFromCache);
|
||||||
|
return responseFromCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try the fallback
|
||||||
|
const fallbackResponse = await caches.match(fallbackUrl);
|
||||||
|
if (fallbackResponse) {
|
||||||
|
console.info('[cozy-sw]: using fallback cached response', fallbackResponse);
|
||||||
|
return fallbackResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const networkFirst = async ({ request, preloadResponsePromise, fallbackUrl }) => {
|
||||||
try {
|
try {
|
||||||
// Try to use the preloaded response, if it's there
|
// Try to use the preloaded response, if it's there
|
||||||
// NOTE: Chrome throws errors regarding preloadResponse, see:
|
// NOTE: Chrome throws errors regarding preloadResponse, see:
|
||||||
|
@ -27,31 +42,27 @@ const networkFirst = async ({ request, preloadResponsePromise, fallbackUrl }) =>
|
||||||
// code along with enableNavigationPreload() and the "activate" listener.
|
// code along with enableNavigationPreload() and the "activate" listener.
|
||||||
const preloadResponse = await preloadResponsePromise;
|
const preloadResponse = await preloadResponsePromise;
|
||||||
if (preloadResponse) {
|
if (preloadResponse) {
|
||||||
console.info('using preload response', preloadResponse);
|
console.info('[cozy-sw]: using preload response', preloadResponse);
|
||||||
putInCache(request, preloadResponse.clone());
|
putInCache(request, preloadResponse.clone());
|
||||||
return preloadResponse;
|
return preloadResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Next try to get the resource from the network
|
// Next try to get the resource from the network
|
||||||
const responseFromNetwork = await fetch(request.clone());
|
const responseFromNetwork = await fetch(request.clone());
|
||||||
// response may be used only once
|
// response may be used only once
|
||||||
// we need to save clone to put one copy in cache
|
// we need to save clone to put one copy in cache
|
||||||
// and serve second one
|
// and serve second one
|
||||||
putInCache(request, responseFromNetwork.clone());
|
if (responseFromNetwork) {
|
||||||
return responseFromNetwork;
|
putInCache(request, responseFromNetwork.clone());
|
||||||
|
console.info('[cozy-sw]: using network response', responseFromNetwork);
|
||||||
|
return responseFromNetwork;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tryCache(request, fallbackUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
// Try get the resource from the cache
|
return tryCache(request, fallbackUrl);
|
||||||
const responseFromCache = await caches.match(request);
|
|
||||||
if (responseFromCache) {
|
|
||||||
return responseFromCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try the fallback
|
|
||||||
const fallbackResponse = await caches.match(fallbackUrl);
|
|
||||||
if (fallbackResponse) {
|
|
||||||
return fallbackResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
// when even the fallback response is not available,
|
// when even the fallback response is not available,
|
||||||
// there is nothing we can do, but we must always
|
// there is nothing we can do, but we must always
|
||||||
|
@ -61,7 +72,6 @@ const networkFirst = async ({ request, preloadResponsePromise, fallbackUrl }) =>
|
||||||
headers: { 'Content-Type': 'text/plain' },
|
headers: { 'Content-Type': 'text/plain' },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const enableNavigationPreload = async () => {
|
const enableNavigationPreload = async () => {
|
||||||
|
|
Loading…
Reference in a new issue