Skip to content

Fix R2 bucket population #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "3.5.4",
"enquirer": "^2.4.1",
"glob": "catalog:"
"glob": "catalog:",
"ts-tqdm": "^0.8.6"
},
"devDependencies": {
"@cloudflare/workers-types": "catalog:",
Expand Down
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
24 changes: 21 additions & 3 deletions packages/cloudflare/src/cli/commands/populate-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {
} from "@opennextjs/aws/types/open-next.js";
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
import { globSync } from "glob";
import { tqdm } from "ts-tqdm";
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 +63,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 }) => {
for (const { fsPath, destPath } of tqdm(assets)) {
const fullDestPath = path.join(
"NEXT_INC_CACHE_R2_BUCKET",
bucket,
process.env.NEXT_INC_CACHE_R2_PREFIX ?? "incremental-cache",
destPath
);
Expand All @@ -78,7 +96,7 @@ export async function populateCache(
// Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
{ target: populateCacheOptions.target, excludeRemoteFlag: true, logging: "error" }
);
});
}
logger.info(`Successfully populated cache with ${assets.length} assets`);
break;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cloudflare/src/cli/utils/run-wrangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts:
{
shell: true,
stdio: wranglerOpts.logging === "error" ? ["ignore", "ignore", "inherit"] : "inherit",
env: {
...process.env,
...(wranglerOpts.logging === "error" ? { WRANGLER_LOG: "error" } : undefined),
},
}
);

Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.