@@ -20,7 +20,7 @@ class Cache implements IncrementalCache {
2020 async get < IsFetch extends boolean = false > (
2121 key : string ,
2222 isFetch ?: IsFetch
23- ) : Promise < WithLastModified < CacheValue < IsFetch > > > {
23+ ) : Promise < WithLastModified < CacheValue < IsFetch > > | null > {
2424 const cfEnv = getCloudflareContext ( ) . env ;
2525 const kv = cfEnv . NEXT_CACHE_WORKERS_KV ;
2626 const assets = cfEnv . ASSETS ;
@@ -43,7 +43,7 @@ class Cache implements IncrementalCache {
4343 const kvKey = this . getKVKey ( key , isFetch ) ;
4444 entry = await kv . get ( kvKey , "json" ) ;
4545 if ( entry ?. status === STATUS_DELETED ) {
46- return { } ;
46+ return null ;
4747 }
4848 }
4949
@@ -61,7 +61,25 @@ class Cache implements IncrementalCache {
6161 lastModified : ( globalThis as { __BUILD_TIMESTAMP_MS__ ?: number } ) . __BUILD_TIMESTAMP_MS__ ,
6262 } ;
6363 }
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+ }
6481 }
82+
6583 this . debug ( entry ? `-> hit` : `-> miss` ) ;
6684 return { value : entry ?. value , lastModified : entry ?. lastModified } ;
6785 } catch {
0 commit comments