Skip to content

Commit 8abea36

Browse files
authored
Fix R2 bucket population (#536)
Fixes #535
1 parent 4ff160f commit 8abea36

File tree

7 files changed

+45
-6
lines changed

7 files changed

+45
-6
lines changed

.changeset/popular-berries-deliver.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 R2 bucket population

packages/cloudflare/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"@dotenvx/dotenvx": "catalog:",
5656
"@opennextjs/aws": "3.5.4",
5757
"enquirer": "^2.4.1",
58-
"glob": "catalog:"
58+
"glob": "catalog:",
59+
"ts-tqdm": "^0.8.6"
5960
},
6061
"devDependencies": {
6162
"@cloudflare/workers-types": "catalog:",

packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class KVIncrementalCache implements IncrementalCache {
140140
}
141141

142142
protected getKVKey(key: string, isFetch?: boolean): string {
143-
return `${this.getBuildId()}/${key}.${isFetch ? "fetch" : "cache"}`;
143+
return `${this.getBuildId()}/${key}.${isFetch ? "fetch" : "cache"}`.replace(/\/+/g, "/");
144144
}
145145

146146
protected getAssetUrl(key: string, isFetch?: boolean): string {

packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ class R2IncrementalCache implements IncrementalCache {
7272
protected getR2Key(key: string, isFetch?: boolean): string {
7373
const directory = getCloudflareContext().env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache";
7474

75-
return `${directory}/${process.env.NEXT_BUILD_ID ?? "no-build-id"}/${key}.${isFetch ? "fetch" : "cache"}`;
75+
return `${directory}/${process.env.NEXT_BUILD_ID ?? "no-build-id"}/${key}.${isFetch ? "fetch" : "cache"}`.replace(
76+
/\/+/g,
77+
"/"
78+
);
7679
}
7780
}
7881

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111
} from "@opennextjs/aws/types/open-next.js";
1212
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
1313
import { globSync } from "glob";
14+
import { tqdm } from "ts-tqdm";
15+
import { unstable_readConfig } from "wrangler";
1416

1517
import { NAME as R2_CACHE_NAME } from "../../api/overrides/incremental-cache/r2-incremental-cache.js";
1618
import { NAME as D1_TAG_NAME } from "../../api/overrides/tag-cache/d1-next-tag-cache.js";
@@ -61,12 +63,28 @@ export async function populateCache(
6163
const name = await resolveCacheName(incrementalCache);
6264
switch (name) {
6365
case R2_CACHE_NAME: {
66+
const config = unstable_readConfig({ env: populateCacheOptions.environment });
67+
68+
const binding = (config.r2_buckets ?? []).find(
69+
({ binding }) => binding === "NEXT_INC_CACHE_R2_BUCKET"
70+
);
71+
72+
if (!binding) {
73+
throw new Error("No R2 binding 'NEXT_INC_CACHE_R2_BUCKET' found!");
74+
}
75+
76+
const bucket = binding.bucket_name;
77+
78+
if (!bucket) {
79+
throw new Error("R2 binding 'NEXT_INC_CACHE_R2_BUCKET' should have a 'bucket_name'");
80+
}
81+
6482
logger.info("\nPopulating R2 incremental cache...");
6583

6684
const assets = getCacheAssetPaths(options);
67-
assets.forEach(({ fsPath, destPath }) => {
85+
for (const { fsPath, destPath } of tqdm(assets)) {
6886
const fullDestPath = path.join(
69-
"NEXT_INC_CACHE_R2_BUCKET",
87+
bucket,
7088
process.env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache",
7189
destPath
7290
);
@@ -78,7 +96,7 @@ export async function populateCache(
7896
// Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
7997
{ target: populateCacheOptions.target, excludeRemoteFlag: true, logging: "error" }
8098
);
81-
});
99+
}
82100
logger.info(`Successfully populated cache with ${assets.length} assets`);
83101
break;
84102
}

packages/cloudflare/src/cli/utils/run-wrangler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts:
7474
{
7575
shell: true,
7676
stdio: wranglerOpts.logging === "error" ? ["ignore", "ignore", "inherit"] : "inherit",
77+
env: {
78+
...process.env,
79+
...(wranglerOpts.logging === "error" ? { WRANGLER_LOG: "error" } : undefined),
80+
},
7781
}
7882
);
7983

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)