mirror of
https://onedev.site.tesses.net/tytd2025
synced 2026-06-01 18:05:32 +00:00
Add pwa support
This commit is contained in:
42
Tesses.YouTubeDownloader.Server/res/service_worker.js
Normal file
42
Tesses.YouTubeDownloader.Server/res/service_worker.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const assets = ["<@ASSETS@>"];
|
||||
|
||||
const staticCacheName = "tytd-static-<@BUILD_TIME@>";
|
||||
|
||||
self.addEventListener('install',evt => {
|
||||
evt.waitUntil(
|
||||
caches.open(staticCacheName).then(cache =>{
|
||||
cache.addAll(assets);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate',(evt)=>{
|
||||
evt.waitUntil(
|
||||
caches.keys().then(keys => {
|
||||
return Promise.all(keys.filter(key => key !== staticCacheName).map(key => caches.delete(key)));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', evt => {
|
||||
const uri = new URL(evt.request.url);
|
||||
|
||||
evt.respondWith(
|
||||
|
||||
caches.match(evt.request).then(cacheRes=>{
|
||||
return cacheRes || fetch(evt.request);
|
||||
}).catch(()=>{
|
||||
if(uri.pathname === '/queue-size')
|
||||
{
|
||||
return new Response('<span hx-trigger="every 5000ms" hx-target="this" hx-push-url="false" hx-indicator="none" hx-get="./queue-size" hx-swap="outerHTML" class="badge">?</span>',{
|
||||
headers: { 'Content-Type': 'text/html' }
|
||||
});
|
||||
}
|
||||
if(uri.pathname === "/progress")
|
||||
{
|
||||
return caches.match("/offline-progress.html");
|
||||
}
|
||||
return caches.match('/offline.html');
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user