Skip to content

Commit

Permalink
Added another filter in sw fetch, #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Elhamne committed Feb 26, 2023
1 parent 5683e8c commit bfa197c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable array-callback-return */
/* eslint-disable no-undef */
const staticCacheName = 'SAY-v2.0.3-beta';
const dynamicCacheName = 'SAY-dynamic-v4';
const dynamicCacheName = 'SAY-dynamic-v5';
const urlsToCache = [
'/',
'/index.html',
Expand Down Expand Up @@ -79,7 +79,10 @@ self.addEventListener('activate', (e) => {

// fetch event
self.addEventListener('fetch', (e) => {
if (e.request.url.indexOf('/api/') === -1) {
if (
e.request.url.indexOf('/api/') === -1 &&
e.request.url.startsWith('http')
) {
e.respondWith(
(async () => {
try {
Expand All @@ -90,12 +93,13 @@ self.addEventListener('fetch', (e) => {
const response = await fetch(e.request);
const cache = await caches.open(dynamicCacheName);
await cache.put(e.request.url, response.clone());
await limitCacheSize(dynamicCacheName, 15);
await limitCacheSize(dynamicCacheName, 50);
return response;
} catch {
if (e.request.url.indexOf('.html') > -1) {
return caches.match('/offline.html');
}
} catch (err) {
console.log('catch', err);
// if (e.request.url.indexOf('.html') > -1) {
// return caches.match('/offline.html');
// }
}
})()
);
Expand Down

0 comments on commit bfa197c

Please sign in to comment.