-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
20 changed files
with
232 additions
and
121 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
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ env.d.ts | |
PingFang-SC-Regular.otf | ||
tsconfig.tsbuildinfo | ||
web.zip | ||
/public/version | ||
/public/version | ||
stats.html |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const CACHE_NAME = 'musiche-cache'; | ||
|
||
const cacheFirst = [ | ||
/(.*?)\.(html|js|json|css|png|jpg|jpeg|woff2|webm|webp)/i, | ||
/\/proxy\?url=[\S]+/i | ||
]; | ||
const routerPattern = | ||
/\/(recommend|yours|ranking|search|playlist|album|lover|recent|local|created|setting|version$)/i; | ||
|
||
self.addEventListener('install', event => { | ||
event.waitUntil(caches.open(CACHE_NAME)); | ||
}); | ||
|
||
self.addEventListener('activate', event => { | ||
event.waitUntil(self.skipWaiting()); | ||
}); | ||
|
||
async function cacheThenNetwork(request) { | ||
const requestUrl = new URL(request.url).toString(); | ||
let willCache = false; | ||
if (!requestUrl.search) { | ||
} | ||
for (let i = 0; i < cacheFirst.length; i++) { | ||
if (cacheFirst[i].test(requestUrl)) { | ||
willCache = true; | ||
break; | ||
} | ||
} | ||
const cache = await caches.open(CACHE_NAME); | ||
const cachedResponse = await cache.match(request); | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
} | ||
const response = await fetch(request); | ||
const cacheRouter = | ||
response.headers.get('content-type')?.includes('text/html') && | ||
routerPattern.test(requestUrl); | ||
if ( | ||
requestUrl.startsWith('http') && | ||
(cacheRouter || (willCache && response.ok)) | ||
) { | ||
try { | ||
const cache = await caches.open(CACHE_NAME); | ||
cache.put(request, response.clone()); | ||
} catch (error) { | ||
console.error('Failed to cache the response:', error); | ||
} | ||
} | ||
return response; | ||
} | ||
|
||
self.addEventListener('fetch', event => { | ||
event.respondWith(cacheThenNetwork(event.request)); | ||
}); | ||
|
||
async function clearAllCache() { | ||
const cache = await caches.open(CACHE_NAME); | ||
const keys = await cache.keys(); | ||
keys.forEach(key => { | ||
cache.delete(key); | ||
}); | ||
self.skipWaiting(); | ||
} | ||
|
||
self.addEventListener('update', event => { | ||
event.waitUntil(clearAllCache()); | ||
}); |
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
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
Oops, something went wrong.