11import { error } from "@opennextjs/aws/adapters/logger.js" ;
2- import { CacheValue , IncrementalCache , WithLastModified } from "@opennextjs/aws/types/overrides.js" ;
2+ import {
3+ CacheEntryType ,
4+ CacheValue ,
5+ IncrementalCache ,
6+ WithLastModified ,
7+ } from "@opennextjs/aws/types/overrides.js" ;
38
49import { getCloudflareContext } from "../../cloudflare-context.js" ;
510import { debugCache , FALLBACK_BUILD_ID , IncrementalCacheEntry } from "../internal.js" ;
@@ -37,8 +42,8 @@ type Options = {
3742
3843interface PutToCacheInput {
3944 key : string ;
40- isFetch : boolean | undefined ;
41- entry : IncrementalCacheEntry < boolean > ;
45+ cacheType ?: CacheEntryType ;
46+ entry : IncrementalCacheEntry < CacheEntryType > ;
4247}
4348
4449/**
@@ -60,13 +65,13 @@ class RegionalCache implements IncrementalCache {
6065 this . opts . shouldLazilyUpdateOnCacheHit ??= this . opts . mode === "long-lived" ;
6166 }
6267
63- async get < IsFetch extends boolean = false > (
68+ async get < CacheType extends CacheEntryType = "cache" > (
6469 key : string ,
65- isFetch ?: IsFetch
66- ) : Promise < WithLastModified < CacheValue < IsFetch > > | null > {
70+ cacheType ?: CacheType
71+ ) : Promise < WithLastModified < CacheValue < CacheType > > | null > {
6772 try {
6873 const cache = await this . getCacheInstance ( ) ;
69- const urlKey = this . getCacheUrlKey ( key , isFetch ) ;
74+ const urlKey = this . getCacheUrlKey ( key , cacheType ) ;
7075
7176 // Check for a cached entry as this will be faster than the store response.
7277 const cachedResponse = await cache . match ( urlKey ) ;
@@ -76,11 +81,11 @@ class RegionalCache implements IncrementalCache {
7681 // Re-fetch from the store and update the regional cache in the background
7782 if ( this . opts . shouldLazilyUpdateOnCacheHit ) {
7883 getCloudflareContext ( ) . ctx . waitUntil (
79- this . store . get ( key , isFetch ) . then ( async ( rawEntry ) => {
84+ this . store . get ( key , cacheType ) . then ( async ( rawEntry ) => {
8085 const { value, lastModified } = rawEntry ?? { } ;
8186
8287 if ( value && typeof lastModified === "number" ) {
83- await this . putToCache ( { key, isFetch , entry : { value, lastModified } } ) ;
88+ await this . putToCache ( { key, cacheType , entry : { value, lastModified } } ) ;
8489 }
8590 } )
8691 ) ;
@@ -89,12 +94,14 @@ class RegionalCache implements IncrementalCache {
8994 return cachedResponse . json ( ) ;
9095 }
9196
92- const rawEntry = await this . store . get ( key , isFetch ) ;
97+ const rawEntry = await this . store . get ( key , cacheType ) ;
9398 const { value, lastModified } = rawEntry ?? { } ;
9499 if ( ! value || typeof lastModified !== "number" ) return null ;
95100
96101 // Update the locale cache after retrieving from the store.
97- getCloudflareContext ( ) . ctx . waitUntil ( this . putToCache ( { key, isFetch, entry : { value, lastModified } } ) ) ;
102+ getCloudflareContext ( ) . ctx . waitUntil (
103+ this . putToCache ( { key, cacheType, entry : { value, lastModified } } )
104+ ) ;
98105
99106 return { value, lastModified } ;
100107 } catch ( e ) {
@@ -103,17 +110,17 @@ class RegionalCache implements IncrementalCache {
103110 }
104111 }
105112
106- async set < IsFetch extends boolean = false > (
113+ async set < CacheType extends CacheEntryType = "cache" > (
107114 key : string ,
108- value : CacheValue < IsFetch > ,
109- isFetch ?: IsFetch
115+ value : CacheValue < CacheType > ,
116+ cacheType ?: CacheType
110117 ) : Promise < void > {
111118 try {
112- await this . store . set ( key , value , isFetch ) ;
119+ await this . store . set ( key , value , cacheType ) ;
113120
114121 await this . putToCache ( {
115122 key,
116- isFetch ,
123+ cacheType ,
117124 entry : {
118125 value,
119126 // Note: `Date.now()` returns the time of the last IO rather than the actual time.
@@ -144,15 +151,13 @@ class RegionalCache implements IncrementalCache {
144151 return this . localCache ;
145152 }
146153
147- protected getCacheUrlKey ( key : string , isFetch ?: boolean ) {
154+ protected getCacheUrlKey ( key : string , cacheType ?: CacheEntryType ) {
148155 const buildId = process . env . NEXT_BUILD_ID ?? FALLBACK_BUILD_ID ;
149- return (
150- "http://cache.local" + `/${ buildId } /${ key } ` . replace ( / \/ + / g, "/" ) + `.${ isFetch ? "fetch" : "cache" } `
151- ) ;
156+ return "http://cache.local" + `/${ buildId } /${ key } ` . replace ( / \/ + / g, "/" ) + `.${ cacheType ?? "cache" } ` ;
152157 }
153158
154- protected async putToCache ( { key, isFetch , entry } : PutToCacheInput ) : Promise < void > {
155- const urlKey = this . getCacheUrlKey ( key , isFetch ) ;
159+ protected async putToCache ( { key, cacheType , entry } : PutToCacheInput ) : Promise < void > {
160+ const urlKey = this . getCacheUrlKey ( key , cacheType ) ;
156161 const cache = await this . getCacheInstance ( ) ;
157162
158163 const age =
@@ -209,7 +214,7 @@ export function withRegionalCache(cache: IncrementalCache, opts: Options) {
209214/**
210215 * Extract the list of tags from a cache entry.
211216 */
212- function getTagsFromCacheEntry ( entry : IncrementalCacheEntry < boolean > ) : string [ ] | undefined {
217+ function getTagsFromCacheEntry ( entry : IncrementalCacheEntry < CacheEntryType > ) : string [ ] | undefined {
213218 if ( "tags" in entry . value && entry . value . tags ) {
214219 return entry . value . tags ;
215220 }
@@ -225,4 +230,7 @@ function getTagsFromCacheEntry(entry: IncrementalCacheEntry<boolean>): string[]
225230 return rawTags . split ( "," ) ;
226231 }
227232 }
233+ if ( "value" in entry . value ) {
234+ return entry . value . tags ;
235+ }
228236}
0 commit comments