1- import type { CacheValue , IncrementalCache , WithLastModified } from "@opennextjs/aws/types/overrides" ;
1+ import type {
2+ CachedFetchValue ,
3+ CacheValue ,
4+ IncrementalCache ,
5+ WithLastModified ,
6+ } from "@opennextjs/aws/types/overrides" ;
27import { IgnorableError , RecoverableError } from "@opennextjs/aws/utils/error.js" ;
38
49import { getCloudflareContext } from "./cloudflare-context.js" ;
@@ -7,23 +12,6 @@ export const CACHE_ASSET_DIR = "cnd-cgi/_next_cache";
712
813export const STATUS_DELETED = 1 ;
914
10- // https://github.com/vercel/next.js/blob/9a1cd356/packages/next/src/server/response-cache/types.ts#L26-L38
11- export type CachedFetchValue = {
12- kind : "FETCH" ;
13- data : {
14- headers : { [ k : string ] : string } ;
15- body : string ;
16- url : string ;
17- status ?: number ;
18- // field used by older versions of Next.js (see: https://github.com/vercel/next.js/blob/fda1ecc/packages/next/src/server/response-cache/types.ts#L23)
19- tags ?: string [ ] ;
20- } ;
21- // tags are only present with file-system-cache
22- // fetch cache stores tags outside of cache entry
23- tags ?: string [ ] ;
24- revalidate : number ;
25- } ;
26-
2715/**
2816 * Open Next cache based on cloudflare KV and Assets.
2917 *
@@ -78,15 +66,17 @@ class Cache implements IncrementalCache {
7866 lastModified : ( globalThis as { __BUILD_TIMESTAMP_MS__ ?: number } ) . __BUILD_TIMESTAMP_MS__ ,
7967 } ;
8068 }
81- }
82-
83- const entryValue = entry ?. value as CachedFetchValue | undefined ;
84- if ( entryValue ?. kind === "FETCH" ) {
85- const expires = entryValue . data . headers ?. expires ;
86- const expiresTime = new Date ( expires as string ) . getTime ( ) ;
87- if ( ! isNaN ( expiresTime ) && expiresTime <= Date . now ( ) ) {
88- this . debug ( `found expired entry (expire time: ${ expires } )` ) ;
89- return null ;
69+ // if we were unable to get the cached data from the KV we get it from the assets generated at build time
70+ // however before serving them we need to make sure that they are not expired, otherwise if the KV cache
71+ // is missing or not working properly we end up always serving the same stale build time generated cache entries
72+ const entryValue = entry ?. value as CachedFetchValue ;
73+ if ( entryValue ?. kind === "FETCH" ) {
74+ const expires = entryValue . data . headers ?. expires ;
75+ const expiresTime = new Date ( expires as string ) . getTime ( ) ;
76+ if ( ! isNaN ( expiresTime ) && expiresTime <= Date . now ( ) ) {
77+ this . debug ( `found expired entry (expire time: ${ expires } )` ) ;
78+ return null ;
79+ }
9080 }
9181 }
9282
0 commit comments