@@ -26,17 +26,38 @@ setGlobalDispatcher(
2626) ;
2727
2828// In-memory cache for docs dev mode to avoid Next.js 2MB cache limit
29- const docsDevCache = new Map < string , FdrAPI . docs . v2 . read . LoadDocsForUrlResponse > ( ) ;
29+ // Stores both the response and the timestamp for time-based invalidation
30+ interface CacheEntry {
31+ response : FdrAPI . docs . v2 . read . LoadDocsForUrlResponse ;
32+ timestamp : number ;
33+ }
34+ const docsDevCache = new Map < string , CacheEntry > ( ) ;
3035const pendingRequests = new Map < string , Promise < FdrAPI . docs . v2 . read . LoadDocsForUrlResponse > > ( ) ;
3136
37+ // Cache TTL for local development (2 seconds) to allow hot reload while maintaining some caching
38+ const LOCAL_CACHE_TTL_MS = 2000 ;
39+
3240export const loadWithUrl = async ( domain : string ) : Promise < FdrAPI . docs . v2 . read . LoadDocsForUrlResponse > => {
33- // In docs dev mode, use in-memory cache instead of unstable_cache
41+ // In docs dev mode (including local CLI) , use in-memory cache with time-based invalidation
3442 if ( isDocsDev ( ) ) {
35- // Check if we have a cached response
43+ const now = Date . now ( ) ;
3644 const cached = docsDevCache . get ( domain ) ;
45+
46+ // For local CLI development, use shorter TTL for hot reload
47+ const ttl = isLocal ( ) ? LOCAL_CACHE_TTL_MS : Infinity ;
48+
49+ // Check if we have a valid cached response (not expired)
50+ if ( cached && now - cached . timestamp < ttl ) {
51+ console . debug ( `[DocsDevCache] Cache hit for domain: ${ domain } (age: ${ now - cached . timestamp } ms)` ) ;
52+ return cached . response ;
53+ }
54+
55+ // If cache is stale, delete it
3756 if ( cached ) {
38- console . debug ( `[DocsDevCache] Cache hit for domain: ${ domain } ` ) ;
39- return cached ;
57+ console . debug (
58+ `[DocsDevCache] Cache expired for domain: ${ domain } (age: ${ now - cached . timestamp } ms, ttl: ${ ttl } ms)`
59+ ) ;
60+ docsDevCache . delete ( domain ) ;
4061 }
4162
4263 // Check if there's already a pending request for this domain
@@ -51,8 +72,8 @@ export const loadWithUrl = async (domain: string): Promise<FdrAPI.docs.v2.read.L
5172 const requestPromise = ( async ( ) => {
5273 try {
5374 const response = await uncachedLoadWithUrl ( domain ) ;
54- // Once resolved, store in cache and remove from pending
55- docsDevCache . set ( domain , response ) ;
75+ // Once resolved, store in cache with timestamp and remove from pending
76+ docsDevCache . set ( domain , { response, timestamp : Date . now ( ) } ) ;
5677 pendingRequests . delete ( domain ) ;
5778 console . debug ( `[DocsDevCache] Cached and cleaned up pending request for domain: ${ domain } ` ) ;
5879 return response ;
0 commit comments