Implement comprehensive cache lifecycle management that includes proper key management, invalidation strategies, and cleanup mechanisms. Cache implementations should handle the full lifecycle from creation to cleanup to prevent memory leaks, stale data, and unbounded growth.
Implement comprehensive cache lifecycle management that includes proper key management, invalidation strategies, and cleanup mechanisms. Cache implementations should handle the full lifecycle from creation to cleanup to prevent memory leaks, stale data, and unbounded growth.
Key practices:
Example implementation:
// Proper cache key management with cleanup
const metaCache = new Map()
const cacheKey = `${absolutePath}:${hash}`
// Handle cache with TTL and error recovery
let manifest: Promise<NuxtAppManifest> | null = null
export function getAppManifest(): Promise<NuxtAppManifest> {
if (!manifest) {
manifest = $fetch(manifestUrl).catch(error => {
manifest = null // Clear on error for retry
throw error
})
}
return manifest
}
// Preserve cache headers for proper browser caching
setResponseHeaders(event, {
'Cache-Control': defaultError.headers['cache-control'],
...defaultError.headers
})
// Implement GC for build caches
// Keep `.cache/nuxt/builds.json` with id + date for cleanup
This approach prevents common caching pitfalls like stale data, memory leaks, and cache corruption while ensuring proper browser caching behavior.
Enter the URL of a public GitHub repository