Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-berries-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Fix R2 bucket population
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class KVIncrementalCache implements IncrementalCache {
}

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

protected getAssetUrl(key: string, isFetch?: boolean): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class R2IncrementalCache implements IncrementalCache {
protected getR2Key(key: string, isFetch?: boolean): string {
const directory = getCloudflareContext().env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache";

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

Expand Down
19 changes: 18 additions & 1 deletion packages/cloudflare/src/cli/commands/populate-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
} from "@opennextjs/aws/types/open-next.js";
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
import { globSync } from "glob";
import { unstable_readConfig } from "wrangler";

import { NAME as R2_CACHE_NAME } from "../../api/overrides/incremental-cache/r2-incremental-cache.js";
import { NAME as D1_TAG_NAME } from "../../api/overrides/tag-cache/d1-next-tag-cache.js";
Expand Down Expand Up @@ -61,12 +62,28 @@ export async function populateCache(
const name = await resolveCacheName(incrementalCache);
switch (name) {
case R2_CACHE_NAME: {
const config = unstable_readConfig({ env: populateCacheOptions.environment });

const binding = (config.r2_buckets ?? []).find(
({ binding }) => binding === "NEXT_INC_CACHE_R2_BUCKET"
);

if (!binding) {
throw new Error("No R2 binding 'NEXT_INC_CACHE_R2_BUCKET' found!");
}

const bucket = binding.bucket_name;

if (!bucket) {
throw new Error("R2 binding 'NEXT_INC_CACHE_R2_BUCKET' should have a 'bucket_name'");
}

logger.info("\nPopulating R2 incremental cache...");

const assets = getCacheAssetPaths(options);
assets.forEach(({ fsPath, destPath }) => {
const fullDestPath = path.join(
"NEXT_INC_CACHE_R2_BUCKET",
bucket,
process.env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache",
destPath
);
Expand Down