refactor(sw): add jsdoc
This commit is contained in:
parent
4b67c33fe9
commit
d8d5b808b7
1 changed files with 23 additions and 0 deletions
23
src/sw.mjs
23
src/sw.mjs
|
@ -5,6 +5,13 @@
|
||||||
*/
|
*/
|
||||||
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleans up old caches by deleting any cache that is not in the allowed list.
|
||||||
|
* This helps prevent the service worker from accumulating unnecessary cached data over time.
|
||||||
|
* @async
|
||||||
|
* @function cleanOldCaches
|
||||||
|
* @returns {Promise<void>} Resolves when all old caches have been deleted
|
||||||
|
*/
|
||||||
const cleanOldCaches = async () => {
|
const cleanOldCaches = async () => {
|
||||||
const allowCacheNames = ['cozy-reader', cacheName]
|
const allowCacheNames = ['cozy-reader', cacheName]
|
||||||
const allCaches = await caches.keys()
|
const allCaches = await caches.keys()
|
||||||
|
@ -16,6 +23,13 @@ const cleanOldCaches = async () => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds resources to the cache with the specified cache name.
|
||||||
|
* @async
|
||||||
|
* @function addResourcesToCache
|
||||||
|
* @param {string[]} resources - An array of resource URLs to be cached.
|
||||||
|
* @returns {Promise<void>} Resolves when all resources have been added to the cache.
|
||||||
|
*/
|
||||||
const addResourcesToCache = async (resources) => {
|
const addResourcesToCache = async (resources) => {
|
||||||
const cache = await caches.open(cacheName)
|
const cache = await caches.open(cacheName)
|
||||||
console.info('adding resources to cache...', resources)
|
console.info('adding resources to cache...', resources)
|
||||||
|
@ -32,6 +46,15 @@ const addResourcesToCache = async (resources) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a response to the cache for a given request.
|
||||||
|
* If a response already exists for the request, it will be replaced.
|
||||||
|
* @async
|
||||||
|
* @function putInCache
|
||||||
|
* @param {Request} request - The request to cache.
|
||||||
|
* @param {Response} response - The response to cache.
|
||||||
|
* @returns {Promise<void>} Resolves when the response has been added to the cache.
|
||||||
|
*/
|
||||||
const putInCache = async (request, response) => {
|
const putInCache = async (request, response) => {
|
||||||
const cache = await caches.open(cacheName)
|
const cache = await caches.open(cacheName)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue