During the development, globalThis resets on every file-save (HMR)
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 22.1k
- Forks
- 1.4k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
Our team uses a caching mechanism to store API responses. We use globalThis.cache and add/retrieve from it.
It works just fine for production. Also when we refresh the page manually on local development, it works fine. The previously cached object is there across page refreshes.
But when we save a file during development, we are very slow. We found out that on each file save, the globalThis gets reset.
This is our Cache file:
import { get } from "Base"
import generateApiUrl from "./GenerateApiUrl"
globalThis.cache = {}
const createHandle = async url => {
let handle = url.replace(/\//ig, "_")
handle = encodeURI(handle)
const encoder = new TextEncoder()
const data = encoder.encode(handle)
if (typeof crypto !== "undefined" && crypto.subtle) {
const hashBuffer = await crypto.subtle.digest("SHA-256", data)
handle = Array.from(new Uint8Array(hashBuffer))
.map(b => b.toString(16).padStart(2, "0"))
.join("")
return handle
} else {
const { createHash } = await import("node:crypto")
handle = createHash("md5").update(handle).digest("hex")
return handle
}
}
const getFromCacheOrApi = async (path, qwikRequestProps) => {
const start = new Date()
const url = generateApiUrl(path, qwikRequestProps)
const handle = await createHandle(url)
if (globalThis.cache?.[handle]) {
console.log(`Getting ${url} from cache ...`)
const data = globalThis.cache[handle]
const end = new Date()
console.log(`Took ${end - start} milliseconds`)
return JSON.parse(data)
}
console.log(`Getting ${url} from API ...`)
const data = await get(path, qwikRequestProps)
const { statusCode } = data || {}
if (!data || statusCode) {
console.log(`Not caching because server returned ${statusCode}\nURL: ${url}\n${JSON.stringify(data)}`)
return data
}
globalThis.cache[handle] = JSON.stringify(data)
const end = new Date()
console.log(`Took ${end - start} milliseconds`)
return data
}
const clearCache = () => {
console.log("Clearing cache ...")
globalThis.cache = {}
console.log("Cleared the cache")
}
export { getFromCacheOrApi }
export { clearCache }
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the inline Cache module and reproduce the reported behavior by saving a file during development while observing globalThis.cache. Trace the HMR lifecycle involved and compare it with a manual page refresh; done means the cache behavior across file saves is explained and covered by a reproducible test or documented limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- developer-experience, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100