@@ -49,17 +49,20 @@ class GitbookIncrementalCache implements IncrementalCache {
4949 const localCacheEntry = await localCache . match ( this . getCacheUrlKey ( cacheKey ) ) ;
5050 if ( localCacheEntry ) {
5151 span . setAttribute ( 'cacheHit' , 'local' ) ;
52- return localCacheEntry . json ( ) ;
52+ const result = ( await localCacheEntry . json ( ) ) as WithLastModified <
53+ CacheValue < CacheType >
54+ > ;
55+ return this . returnNullOn404 ( result ) ;
5356 }
5457
5558 const r2Object = await r2 . get ( cacheKey ) ;
5659 if ( ! r2Object ) return null ;
5760
5861 span . setAttribute ( 'cacheHit' , 'r2' ) ;
59- return {
62+ return this . returnNullOn404 ( {
6063 value : await r2Object . json ( ) ,
6164 lastModified : r2Object . uploaded . getTime ( ) ,
62- } ;
65+ } ) ;
6366 } catch ( e ) {
6467 console . error ( 'Failed to get from cache' , e ) ;
6568 return null ;
@@ -68,6 +71,18 @@ class GitbookIncrementalCache implements IncrementalCache {
6871 ) ;
6972 }
7073
74+ //TODO: This is a workaround to handle 404 responses in the cache.
75+ // It should be handled by OpenNext cache interception directly. This should be removed once OpenNext cache interception is fixed.
76+ returnNullOn404 < CacheType extends CacheEntryType = 'cache' > (
77+ cacheEntry : WithLastModified < CacheValue < CacheType > > | null
78+ ) : WithLastModified < CacheValue < CacheType > > | null {
79+ if ( ! cacheEntry ?. value ) return null ;
80+ if ( 'meta' in cacheEntry . value && cacheEntry . value . meta ?. status === 404 ) {
81+ return null ;
82+ }
83+ return cacheEntry ;
84+ }
85+
7186 async set < CacheType extends CacheEntryType = 'cache' > (
7287 key : string ,
7388 value : CacheValue < CacheType > ,
0 commit comments