Skip to content

Instantly share code, notes, and snippets.

@cademcniven
Created July 26, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cademcniven/9278f1e7cac02540a1f49cd0e6cf1576 to your computer and use it in GitHub Desktop.
Save cademcniven/9278f1e7cac02540a1f49cd0e6cf1576 to your computer and use it in GitHub Desktop.
Unfinished code for a fetch wrapper with caching
class Cetch {
static MAX_CACHE_SIZE
static CACHE_VERSION
static CACHE_PREFIX
static CACHE_NAME
static MAX_CACHE_SIZE
static STATIC_ASSETS
constructor() {
this.MAX_CACHE_SIZE = 0
this.CACHE_VERSION = 0
this.CACHE_PREFIX = 'site-cache'
this.CACHE_NAME = `${this.cachePrefix}-${this.cacheVersion}`
this.MAX_CACHE_SIZE = 0
this.STATIC_ASSETS = [
'/',
'/webnovel.js',
'/chapter.js',
'/registerServiceWorker.js',
'/site.css',
'/img/favicon-16x16.png',
'/img/favicon-32x32.png',
'/img/ios/144.png'
]
this.#cacheStaticAssets()
}
async #cacheStaticAssets() {
caches.open(this.CACHE_NAME).then(cache => {
let now = Date.now()
let cachePromises = this.STATIC_ASSETS.map(urlToPrefetch => {
let url = new URL(urlToPrefetch, location.href)
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now
let request = new Request(url, { mode: 'no-cors' })
return fetch(request).then(response => {
if (response.status >= 400) {
throw new Error(`Request for ${urlToPrefetch} failed with status ${response.statusText}`)
}
return cache.put(urlToPrefetch, response)
}).catch(error => {
console.error(`Not caching ${urlToPrefetch} due to ${error}`)
})
})
return Promise.all(cachePromises).then(() => {
console.log('Static assets cached')
})
}).catch(error => {
console.error('Failed to cache static assets:', error)
})
}
get cacheVersion() {
return this.cacheVersion
}
get maxCacheSize() {
return this.MAX_CACHE_SIZE
}
set maxCacheSize(maxCacheSize) {
if (!Number.isSafeInteger(maxCacheSize))
return
this.MAX_CACHE_SIZE = maxCacheSize
}
get cacheVersion() {
return this.CACHE_VERSION
}
set cacheVersion(cacheVersion) {
this.CACHE_VERSION = cacheVersion
}
get cachePrefix() {
return this.CACHE_PREFIX
}
set cachePrefix(cachePrefix) {
this.CACHE_PREFIX = cachePrefix
}
get cacheName() {
return this.CACHE_NAME
}
async fetch(url, options = {}) {
let cachedData = await this.#getCachedData(url)
if (cachedData) {
console.log('Retrieved cached data', cachedData)
return cachedData
}
const cacheStorage = await caches.open(this.CACHE_NAME)
await cacheStorage.add(url)
console.log('Fetching fresh data')
// cachedData = await this.#getCachedData(this.CACHE_NAME, url)
// await this.#deleteOldCaches()
return fetch(url, options)
}
async #getCachedData(url) {
const cacheStorage = await caches.open(this.CACHE_NAME)
const cachedResponse = await cacheStorage.match(url)
if (!cachedResponse || !cachedResponse.ok)
return false
return await cachedResponse.json()
}
async #deleteOldCaches() {
const keys = await caches.keys()
for (const key of keys) {
const isOurCache = `${this.CACHE_PREFIX}-` ===
key.substring(0, this.CACHE_PREFIX.length + 1)
if (currentCache === key || !isOurCache)
continue
caches.delete(key)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment