Skip to content

Commit cb3a2f2

Browse files
conico974vicb
andauthored
fix cache population hanging (#648)
* fix cache population hanging * review fix * fixup! types --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 1e92d6a commit cb3a2f2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

.changeset/major-comics-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix cache population hanging

packages/cloudflare/src/cli/commands/populate-cache.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
1313
import { globSync } from "glob";
1414
import { tqdm } from "ts-tqdm";
15-
import { getPlatformProxy, unstable_readConfig } from "wrangler";
15+
import { getPlatformProxy, type GetPlatformProxyOptions, unstable_readConfig } from "wrangler";
1616

1717
import {
1818
BINDING_NAME as KV_CACHE_BINDING_NAME,
@@ -92,14 +92,20 @@ export function getCacheAssets(opts: BuildOptions): CacheAsset[] {
9292
return assets;
9393
}
9494

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+
95102
async function populateR2IncrementalCache(
96103
options: BuildOptions,
97104
populateCacheOptions: { target: WranglerTarget; environment?: string }
98105
) {
99106
logger.info("\nPopulating R2 incremental cache...");
100107

101108
const config = unstable_readConfig({ env: populateCacheOptions.environment });
102-
const proxy = await getPlatformProxy<CloudflareEnv>(populateCacheOptions);
103109

104110
const binding = config.r2_buckets.find(({ binding }) => binding === R2_CACHE_BINDING_NAME);
105111
if (!binding) {
@@ -111,15 +117,16 @@ async function populateR2IncrementalCache(
111117
throw new Error(`R2 binding ${JSON.stringify(R2_CACHE_BINDING_NAME)} should have a 'bucket_name'`);
112118
}
113119

120+
const prefix = await getPlatformProxyEnv(populateCacheOptions, R2_CACHE_PREFIX_ENV_NAME);
121+
114122
const assets = getCacheAssets(options);
115123

116124
for (const { fullPath, key, buildId, isFetch } of tqdm(assets)) {
117125
const cacheKey = computeCacheKey(key, {
118-
prefix: proxy.env[R2_CACHE_PREFIX_ENV_NAME],
126+
prefix,
119127
buildId,
120128
cacheType: isFetch ? "fetch" : "cache",
121129
});
122-
123130
runWrangler(
124131
options,
125132
["r2 object put", quoteShellMeta(path.join(bucket, cacheKey)), `--file ${quoteShellMeta(fullPath)}`],
@@ -138,13 +145,14 @@ async function populateKVIncrementalCache(
138145
logger.info("\nPopulating KV incremental cache...");
139146

140147
const config = unstable_readConfig({ env: populateCacheOptions.environment });
141-
const proxy = await getPlatformProxy<CloudflareEnv>(populateCacheOptions);
142148

143149
const binding = config.kv_namespaces.find(({ binding }) => binding === KV_CACHE_BINDING_NAME);
144150
if (!binding) {
145151
throw new Error(`No KV binding ${JSON.stringify(KV_CACHE_BINDING_NAME)} found!`);
146152
}
147153

154+
const prefix = await getPlatformProxyEnv(populateCacheOptions, KV_CACHE_PREFIX_ENV_NAME);
155+
148156
const assets = getCacheAssets(options);
149157

150158
const chunkSize = Math.max(1, populateCacheOptions.cacheChunkSize ?? 25);
@@ -159,7 +167,7 @@ async function populateKVIncrementalCache(
159167
.slice(i * chunkSize, (i + 1) * chunkSize)
160168
.map(({ fullPath, key, buildId, isFetch }) => ({
161169
key: computeCacheKey(key, {
162-
prefix: proxy.env[KV_CACHE_PREFIX_ENV_NAME],
170+
prefix,
163171
buildId,
164172
cacheType: isFetch ? "fetch" : "cache",
165173
}),

0 commit comments

Comments
 (0)