@@ -20,7 +20,7 @@ class Cache implements IncrementalCache {
20
20
async get < IsFetch extends boolean = false > (
21
21
key : string ,
22
22
isFetch ?: IsFetch
23
- ) : Promise < WithLastModified < CacheValue < IsFetch > > > {
23
+ ) : Promise < WithLastModified < CacheValue < IsFetch > > | null > {
24
24
const cfEnv = getCloudflareContext ( ) . env ;
25
25
const kv = cfEnv . NEXT_CACHE_WORKERS_KV ;
26
26
const assets = cfEnv . ASSETS ;
@@ -43,7 +43,7 @@ class Cache implements IncrementalCache {
43
43
const kvKey = this . getKVKey ( key , isFetch ) ;
44
44
entry = await kv . get ( kvKey , "json" ) ;
45
45
if ( entry ?. status === STATUS_DELETED ) {
46
- return { } ;
46
+ return null ;
47
47
}
48
48
}
49
49
@@ -61,7 +61,25 @@ class Cache implements IncrementalCache {
61
61
lastModified : ( globalThis as { __BUILD_TIMESTAMP_MS__ ?: number } ) . __BUILD_TIMESTAMP_MS__ ,
62
62
} ;
63
63
}
64
+ if ( ! kv ) {
65
+ // The cache can not be updated when there is no KV
66
+ // As we don't want to keep serving stale data for ever,
67
+ // we pretend the entry is not in cache
68
+ if (
69
+ entry ?. value &&
70
+ "kind" in entry . value &&
71
+ entry . value . kind === "FETCH" &&
72
+ entry . value . data ?. headers ?. expires
73
+ ) {
74
+ const expiresTime = new Date ( entry . value . data . headers . expires ) . getTime ( ) ;
75
+ if ( ! isNaN ( expiresTime ) && expiresTime <= Date . now ( ) ) {
76
+ this . debug ( `found expired entry (expire time: ${ entry . value . data . headers . expires } )` ) ;
77
+ return null ;
78
+ }
79
+ }
80
+ }
64
81
}
82
+
65
83
this . debug ( entry ? `-> hit` : `-> miss` ) ;
66
84
return { value : entry ?. value , lastModified : entry ?. lastModified } ;
67
85
} catch {
0 commit comments