diff --git a/packages/gitbook-v2/openNext/incrementalCache.ts b/packages/gitbook-v2/openNext/incrementalCache.ts index 28d1d6b8a..8e4ca9863 100644 --- a/packages/gitbook-v2/openNext/incrementalCache.ts +++ b/packages/gitbook-v2/openNext/incrementalCache.ts @@ -49,17 +49,20 @@ class GitbookIncrementalCache implements IncrementalCache { const localCacheEntry = await localCache.match(this.getCacheUrlKey(cacheKey)); if (localCacheEntry) { span.setAttribute('cacheHit', 'local'); - return localCacheEntry.json(); + const result = (await localCacheEntry.json()) as WithLastModified< + CacheValue + >; + return this.returnNullOn404(result); } const r2Object = await r2.get(cacheKey); if (!r2Object) return null; span.setAttribute('cacheHit', 'r2'); - return { + return this.returnNullOn404({ value: await r2Object.json(), lastModified: r2Object.uploaded.getTime(), - }; + }); } catch (e) { console.error('Failed to get from cache', e); return null; @@ -68,6 +71,18 @@ class GitbookIncrementalCache implements IncrementalCache { ); } + //TODO: This is a workaround to handle 404 responses in the cache. + // It should be handled by OpenNext cache interception directly. This should be removed once OpenNext cache interception is fixed. + returnNullOn404( + cacheEntry: WithLastModified> | null + ): WithLastModified> | null { + if (!cacheEntry?.value) return null; + if ('meta' in cacheEntry.value && cacheEntry.value.meta?.status === 404) { + return null; + } + return cacheEntry; + } + async set( key: string, value: CacheValue,