11/* eslint-disable @typescript-eslint/no-unused-vars */
22import type { KVNamespace } from "@cloudflare/workers-types" ;
3- import type { Extension } from "@opennextjs/aws/types/cache" ;
43import type { CacheValue , IncrementalCache , WithLastModified } from "@opennextjs/aws/types/overrides" ;
54import { IgnorableError , RecoverableError } from "@opennextjs/aws/utils/error.js" ;
65
@@ -39,7 +38,7 @@ class Cache implements IncrementalCache {
3938
4039 try {
4140 this . debug ( `- From KV` ) ;
42- const kvKey = this . getKVKey ( key , isFetch ? "fetch" : "cache" ) ;
41+ const kvKey = this . getKVKey ( key , isFetch ) ;
4342
4443 let entry = ( await this . kv ?. get ( kvKey , "json" ) ) as {
4544 value ?: CacheValue < IsFetch > ;
@@ -50,7 +49,7 @@ class Cache implements IncrementalCache {
5049 return { } ;
5150 }
5251 if ( ! entry && this . assets ) {
53- const url = this . getAssetUrl ( key ) ;
52+ const url = this . getAssetUrl ( key , isFetch ) ;
5453 const response = await this . assets . fetch ( url ) ;
5554 this . debug ( `- From Assets` ) ;
5655 if ( response . ok ) {
@@ -84,7 +83,7 @@ class Cache implements IncrementalCache {
8483 }
8584 this . debug ( `Set ${ key } ` ) ;
8685 try {
87- const kvKey = this . getKVKey ( key , isFetch ? "fetch" : "cache" ) ;
86+ const kvKey = this . getKVKey ( key , isFetch ) ;
8887 // Note: We can not set a TTL as we might fallback to assets,
8988 // still removing old data (old BUILD_ID) could help avoiding
9089 // the cache growing too big.
@@ -109,20 +108,22 @@ class Cache implements IncrementalCache {
109108 }
110109 this . debug ( `Delete ${ key } ` ) ;
111110 try {
112- const kvKey = this . getKVKey ( key , "cache" ) ;
111+ const kvKey = this . getKVKey ( key , /* isFetch= */ false ) ;
113112 // Do not delete the key as we will then fallback to the assets.
114113 await this . kv . put ( kvKey , JSON . stringify ( { status : STATUS_DELETED } ) ) ;
115114 } catch ( e ) {
116115 throw new RecoverableError ( `Failed to delete cache [${ key } ]` ) ;
117116 }
118117 }
119118
120- protected getKVKey ( key : string , extension : Extension ) : string {
121- return `${ this . getBuildId ( ) } /${ key } .${ extension } ` ;
119+ protected getKVKey ( key : string , isFetch ?: boolean ) : string {
120+ return `${ this . getBuildId ( ) } /${ key } .${ isFetch ? "fetch" : "cache" } ` ;
122121 }
123122
124- protected getAssetUrl ( key : string ) : string {
125- return `http://assets.local/${ CACHE_ASSET_DIR } /${ this . getBuildId ( ) } /${ key } .cache` . replace ( / \/ \/ / g, "/" ) ;
123+ protected getAssetUrl ( key : string , isFetch ?: boolean ) : string {
124+ return isFetch
125+ ? `http://assets.local/${ CACHE_ASSET_DIR } /__fetch/${ this . getBuildId ( ) } /${ key } `
126+ : `http://assets.local/${ CACHE_ASSET_DIR } /${ this . getBuildId ( ) } /${ key } .cache` ;
126127 }
127128
128129 protected debug ( ...args : unknown [ ] ) {
0 commit comments