Skip to content

Commit

Permalink
fix: uncommented checkCache
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Apr 28, 2024
1 parent 99f07a9 commit 7372303
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

const cacheName = "dynamic-v2";
const updatedOn = new Date('2024-04-28T03:15:28.668Z');

let organization_id = ""
let storage = true

Expand All @@ -46,14 +48,23 @@ self.addEventListener("fetch", async (e) => {
caches
.match(e.request)
.then(async (cacheResponse) => {
if (!navigator.onLine || !!cacheResponse && cacheType !== 'false') {
const organization = cacheResponse.headers.get('organization')
const lastModified = cacheResponse.headers.get('last-modified')
let fetchedOn
if (cacheResponse && cacheType !== 'false') {
fetchedOn = cacheResponse.headers.get('fetched-on')
if (fetchedOn)
fetchedOn = new Date(fetchedOn)
}

// returnedFromCache[e.request.url] = { organization, lastModified }
const organization = cacheResponse.headers.get('organization')

if (!navigator.onLine || !!cacheResponse && cacheType !== 'false' && (!fetchedOn && !organization || fetchedOn > updatedOn)) {
console.log('servering cache fetchOn greater', fetchedOn > updatedOn, organization)
const lastModified = cacheResponse.headers.get('last-modified')
sendCacheUpdate(e.request.url, organization, lastModified);
return cacheResponse;
} else {
console.log('fetching fetchOn less', fetchedOn > updatedOn)

const networkResponse = await fetch(e.request);

if (!organization_id)
Expand Down Expand Up @@ -178,7 +189,7 @@ const sendCacheUpdate = (url, organization, lastModified) => {
});

fetchTimeout = null;
returnedFromCache = {};
// returnedFromCache = {};
}, 500);
};

Expand Down

0 comments on commit 7372303

Please sign in to comment.