Skip to content

During the development, globalThis resets on every file-save (HMR) #7640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Nefcanto opened this issue May 25, 2025 · 0 comments
Open

During the development, globalThis resets on every file-save (HMR) #7640

Nefcanto opened this issue May 25, 2025 · 0 comments

Comments

@Nefcanto
Copy link

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 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant