-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eed563b
commit ba0f392
Showing
2 changed files
with
25 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,29 @@ | ||
const CACHE_NAME = 'gym-live-v1'; | ||
const URLS_TO_CACHE = [ | ||
'/', | ||
'/manifest.json', | ||
'/assets/app.css', | ||
'/assets/app.js', | ||
'/images/icons/icon-72x72.png', | ||
'/images/icons/icon-96x96.png', | ||
'/images/icons/icon-128x128.png', | ||
'/images/icons/icon-144x144.png', | ||
'/images/icons/icon-152x152.png', | ||
'/images/icons/icon-192x192.png', | ||
'/images/icons/icon-384x384.png', | ||
'/images/icons/icon-512x512.png' | ||
]; | ||
const sw = self; | ||
const config = { | ||
debug: false, | ||
} | ||
|
||
self.addEventListener('install', (event) => { | ||
event.waitUntil( | ||
caches.open(CACHE_NAME) | ||
.then((cache) => { | ||
return cache.addAll(URLS_TO_CACHE); | ||
}) | ||
); | ||
}); | ||
const { debug } = config; | ||
|
||
self.addEventListener('fetch', (event) => { | ||
event.respondWith( | ||
caches.match(event.request) | ||
.then((response) => { | ||
// Cache hit - return response | ||
if (response) { | ||
return response; | ||
} | ||
sw.addEventListener('install', handleInstall); | ||
|
||
return fetch(event.request).then( | ||
(response) => { | ||
// Check if we received a valid response | ||
if (!response || response.status !== 200 || response.type !== 'basic') { | ||
return response; | ||
} | ||
function handleInstall(event) { | ||
debug && console.log('[Service Worker] Installed.'); | ||
} | ||
|
||
// Clone the response | ||
const responseToCache = response.clone(); | ||
sw.addEventListener('fetch', handleFetch); | ||
|
||
caches.open(CACHE_NAME) | ||
.then((cache) => { | ||
cache.put(event.request, responseToCache); | ||
}); | ||
function handleFetch(event) { | ||
// Ignore non-GET requests | ||
if (event.request.method.toUpperCase() !== 'GET') { return; } | ||
|
||
return response; | ||
} | ||
); | ||
}) | ||
); | ||
}); | ||
// Ignore requests from Chrome extensions | ||
if (event.request.url.startsWith("chrome-extension://")) { return; } | ||
|
||
const url = new URL(event.request.url); | ||
// Ignore LiveReloader - only requested in dev | ||
if (url.pathname === "/phoenix/live_reload/frame") { return; } | ||
|
||
debug && console.log('[Service Worker] Handling fetch:', event.request.url); | ||
event.respondWith(fetch(event.request)); | ||
} |