Skip to content

Commit

Permalink
fix(cache): invalidate entry before calling resolver (unjs#271)
Browse files Browse the repository at this point in the history
* fix(cache): prevent using invalid response from cache

* Update src/runtime/cache.ts

* invalidate all fields

Co-authored-by: Clark Du <cdu@hubspot.com>
Co-authored-by: pooya parsa <pyapar@gmail.com>
  • Loading branch information
3 people authored and WinterYukky committed Nov 1, 2022
1 parent 679c622 commit 3c09d77
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export function defineCachedFunction <T=any> (fn: ((...args) => T | Promise<T>),

const _resolve = async () => {
if (!pending[key]) {
// Remove cached entry to prevent using expired cache on concurrent requests
entry.value = undefined
entry.integrity = undefined
entry.mtime = undefined
entry.expires = undefined
pending[key] = Promise.resolve(resolver())
}
entry.value = await pending[key]
Expand Down

0 comments on commit 3c09d77

Please sign in to comment.