@@ -12,7 +12,7 @@ import type {
12
12
import type { IncrementalCache , TagCache } from "@opennextjs/aws/types/overrides.js" ;
13
13
import { globSync } from "glob" ;
14
14
import { tqdm } from "ts-tqdm" ;
15
- import { getPlatformProxy , unstable_readConfig } from "wrangler" ;
15
+ import { getPlatformProxy , type GetPlatformProxyOptions , unstable_readConfig } from "wrangler" ;
16
16
17
17
import {
18
18
BINDING_NAME as KV_CACHE_BINDING_NAME ,
@@ -92,14 +92,20 @@ export function getCacheAssets(opts: BuildOptions): CacheAsset[] {
92
92
return assets ;
93
93
}
94
94
95
+ async function getPlatformProxyEnv < T extends keyof CloudflareEnv > ( options : GetPlatformProxyOptions , key : T ) {
96
+ const proxy = await getPlatformProxy < CloudflareEnv > ( options ) ;
97
+ const prefix = proxy . env [ key ] ;
98
+ await proxy . dispose ( ) ;
99
+ return prefix ;
100
+ }
101
+
95
102
async function populateR2IncrementalCache (
96
103
options : BuildOptions ,
97
104
populateCacheOptions : { target : WranglerTarget ; environment ?: string }
98
105
) {
99
106
logger . info ( "\nPopulating R2 incremental cache..." ) ;
100
107
101
108
const config = unstable_readConfig ( { env : populateCacheOptions . environment } ) ;
102
- const proxy = await getPlatformProxy < CloudflareEnv > ( populateCacheOptions ) ;
103
109
104
110
const binding = config . r2_buckets . find ( ( { binding } ) => binding === R2_CACHE_BINDING_NAME ) ;
105
111
if ( ! binding ) {
@@ -111,15 +117,16 @@ async function populateR2IncrementalCache(
111
117
throw new Error ( `R2 binding ${ JSON . stringify ( R2_CACHE_BINDING_NAME ) } should have a 'bucket_name'` ) ;
112
118
}
113
119
120
+ const prefix = await getPlatformProxyEnv ( populateCacheOptions , R2_CACHE_PREFIX_ENV_NAME ) ;
121
+
114
122
const assets = getCacheAssets ( options ) ;
115
123
116
124
for ( const { fullPath, key, buildId, isFetch } of tqdm ( assets ) ) {
117
125
const cacheKey = computeCacheKey ( key , {
118
- prefix : proxy . env [ R2_CACHE_PREFIX_ENV_NAME ] ,
126
+ prefix,
119
127
buildId,
120
128
cacheType : isFetch ? "fetch" : "cache" ,
121
129
} ) ;
122
-
123
130
runWrangler (
124
131
options ,
125
132
[ "r2 object put" , quoteShellMeta ( path . join ( bucket , cacheKey ) ) , `--file ${ quoteShellMeta ( fullPath ) } ` ] ,
@@ -138,13 +145,14 @@ async function populateKVIncrementalCache(
138
145
logger . info ( "\nPopulating KV incremental cache..." ) ;
139
146
140
147
const config = unstable_readConfig ( { env : populateCacheOptions . environment } ) ;
141
- const proxy = await getPlatformProxy < CloudflareEnv > ( populateCacheOptions ) ;
142
148
143
149
const binding = config . kv_namespaces . find ( ( { binding } ) => binding === KV_CACHE_BINDING_NAME ) ;
144
150
if ( ! binding ) {
145
151
throw new Error ( `No KV binding ${ JSON . stringify ( KV_CACHE_BINDING_NAME ) } found!` ) ;
146
152
}
147
153
154
+ const prefix = await getPlatformProxyEnv ( populateCacheOptions , KV_CACHE_PREFIX_ENV_NAME ) ;
155
+
148
156
const assets = getCacheAssets ( options ) ;
149
157
150
158
const chunkSize = Math . max ( 1 , populateCacheOptions . cacheChunkSize ?? 25 ) ;
@@ -159,7 +167,7 @@ async function populateKVIncrementalCache(
159
167
. slice ( i * chunkSize , ( i + 1 ) * chunkSize )
160
168
. map ( ( { fullPath, key, buildId, isFetch } ) => ( {
161
169
key : computeCacheKey ( key , {
162
- prefix : proxy . env [ KV_CACHE_PREFIX_ENV_NAME ] ,
170
+ prefix,
163
171
buildId,
164
172
cacheType : isFetch ? "fetch" : "cache" ,
165
173
} ) ,
0 commit comments