@@ -111,12 +111,15 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
111
111
entry . mtime = Date . now ( ) ;
112
112
entry . integrity = integrity ;
113
113
delete pending [ key ] ;
114
- const promise =
115
- validate ( entry ) === false
116
- ? useStorage ( ) . removeItem ( cacheKey )
117
- : useStorage ( ) . setItem ( cacheKey , entry ) ;
114
+ const isValid = validate ( entry ) !== false ;
115
+ const promise = isValid
116
+ ? useStorage ( ) . setItem ( cacheKey , entry )
117
+ : useStorage ( ) . removeItem ( cacheKey ) ;
118
118
promise . catch ( ( error ) => {
119
- console . error ( `[nitro] [cache] Cache update error.` , error ) ;
119
+ console . error (
120
+ `[nitro] [cache] Cache ${ isValid ? "update" : "removal" } error.` ,
121
+ error
122
+ ) ;
120
123
useNitroApp ( ) . captureError ( error , { event, tags : [ "cache" ] } ) ;
121
124
} ) ;
122
125
if ( event ?. waitUntil ) {
@@ -137,18 +140,6 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
137
140
_resolvePromise . catch ( ( error ) => {
138
141
console . error ( `[nitro] [cache] SWR handler error.` , error ) ;
139
142
useNitroApp ( ) . captureError ( error , { event, tags : [ "cache" ] } ) ;
140
-
141
- // SWR revalidation failed, remove existing entry so we do
142
- // not continue to return the cached value indefinitely.
143
- const promise = useStorage ( )
144
- . removeItem ( cacheKey )
145
- . catch ( ( error ) => {
146
- console . error ( `[nitro] [cache] Cache write error.` , error ) ;
147
- useNitroApp ( ) . captureError ( error , { event, tags : [ "cache" ] } ) ;
148
- } ) ;
149
- if ( event ?. waitUntil ) {
150
- event . waitUntil ( promise ) ;
151
- }
152
143
} ) ;
153
144
return entry ;
154
145
}
0 commit comments