Skip to content

Commit 2863582

Browse files
committed
Fix R2 bucket population
Fixes #535
1 parent 4ff160f commit 2863582

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
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/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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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 { unstable_readConfig } from "wrangler";
1415

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

6683
const assets = getCacheAssetPaths(options);
6784
assets.forEach(({ fsPath, destPath }) => {
6885
const fullDestPath = path.join(
69-
"NEXT_INC_CACHE_R2_BUCKET",
86+
bucket,
7087
process.env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache",
7188
destPath
7289
);

0 commit comments

Comments
 (0)