Skip to content

feat: use remote dev for r2 cache population#1159

Merged
edmundhung merged 10 commits intomainfrom
edmundhung/r2-remote-cache-population-clean
Mar 26, 2026
Merged

feat: use remote dev for r2 cache population#1159
edmundhung merged 10 commits intomainfrom
edmundhung/r2-remote-cache-population-clean

Conversation

@edmundhung
Copy link
Copy Markdown
Contributor

@edmundhung edmundhung commented Mar 20, 2026

Fixes #1110, #1088
Close #1121, #1099

This is based on the work @vicb and @isaacrowntree did in the PRs above, with the following changes:

  • Use Wrangler remote dev for a more reliable remote R2 cache population path as remote bindings fail under large payload volume (Shared WebSocket RPC sessions in workerd seem to fail under large payload volume cloudflare/capnweb#158)
  • Auto-provision the R2 bucket when remote cache population runs and the bucket does not exist
  • Set config: "" when starting the helper worker to avoid auto-discovering the project wrangler config and leaking unrelated bindings into the cache-populate worker
  • gradually ramp up concurrency to avoid overwhelming the worker
  • simplify the upload request to reduce overhead during cache population
  • add retry/backoff for upstream 5xx responses

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest commit: fbdae21

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 20, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@1159

commit: fbdae21

@edmundhung edmundhung force-pushed the edmundhung/r2-remote-cache-population-clean branch 2 times, most recently from f037821 to b1085e0 Compare March 20, 2026 15:26
@grabmateusz
Copy link
Copy Markdown

grabmateusz commented Mar 24, 2026

Hi @edmundhung, I've decided to give it a go and unfortunately current state of PR does not resolve #1110
It fails on our end with:

Populating remote R2 incremental cache...
Monorepo detected at <path>
  0%|                                             | 1/9599 [00:00:<00:00:, 0.00it/s]
  0%|                                             | 2/9599 [00:00:<00:23:, 400.00it/s]

Some additional progress and:

  1%|                                             | 129/9599 [00:13:<16:11:, 9.75it/s]
ERROR Failed to populate remote R2 bucket "<bucket>" for binding "NEXT_INC_CACHE_R2_BUCKET": fetch failed

Or

Populating remote R2 incremental cache...
Monorepo detected at <path>
  0%|                                             | 1/123 [00:00:<00:00:, 0.00it/s]
  1%|                                             | 2/123 [00:00:<00:00:, 400.00it/s]

Some additional progress and:

 21%|█████████                                    | 26/123 [00:00:<00:00:, 1733.33it/s]
ERROR Failed to populate remote R2 bucket "<bucket>" for binding "NEXT_INC_CACHE_R2_BUCKET": Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Tested on Next.js 15.5.14 and 16.1.6.

edmundhung and others added 4 commits March 24, 2026 16:22
Use Wrangler remote dev for remote R2 cache population, ensure the target bucket exists before uploading cache entries, and isolate the helper worker from project config discovery.

Co-authored-by: Victor Berchet <victor@suumit.com>
Co-authored-by: Isaac Rowntree <isaac@rowntree.me>
@edmundhung edmundhung force-pushed the edmundhung/r2-remote-cache-population-clean branch from b1085e0 to 92b3cf2 Compare March 24, 2026 16:22
@edmundhung
Copy link
Copy Markdown
Contributor Author

@grabmateusz Thanks for giving this a try 🙏🏼

I have made a few additional changes to make the remote cache populate path more resilient. Would you mind giving the pre-release another try?

npm i https://pkg.pr.new/@opennextjs/cloudflare@92b3cf2

If it still fails, could you also try lowering --cache-chunk-size from the default 25 to something smaller like 10 or 5 and let me know whether that fixes the issue?

@grabmateusz
Copy link
Copy Markdown

Hi @edmundhung, this looks much better, I've run 6 parallel deployments (without adjusting --cache-chunk-size) and results are positive now, among them rather big ISR caches were populated successfully:

100%|█████████████████████████████████████████████| 9599/9599 [01:19:<00:00:, 121.05it/s]
Successfully populated cache with 9599 entries

or

100%|█████████████████████████████████████████████| 12859/12859 [03:21:<00:00:, 63.91it/s]
Successfully populated cache with 12859 entries

Test performed so far only on Next.js 15.5.14 but looks promising, will update this comment tomorrow with Next.js 16.1.6 test results.

Copy link
Copy Markdown
Collaborator

@james-elicx james-elicx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been through some of the logic and it looks good / makes sense. Just have a couple of minor questions/notes.

Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/utils/ensure-r2-bucket.ts
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts
@edmundhung edmundhung requested review from petebacondarwin and removed request for dario-piotrowicz March 25, 2026 15:48
Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of comments. Hope they are helpful.

Comment thread .changeset/fix-r2-cache-upload.md Outdated
Comment thread packages/cloudflare/src/cli/commands/deploy.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.spec.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.spec.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/commands/populate-cache.ts Outdated
Comment thread packages/cloudflare/src/cli/workers/r2-cache.ts Outdated
Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing all my feedback

@edmundhung
Copy link
Copy Markdown
Contributor Author

edmundhung commented Mar 25, 2026

Thanks for the review, @petebacondarwin and @james-elicx!

@edmundhung
Copy link
Copy Markdown
Contributor Author

@grabmateusz We are ready to ship this. If you get a chance, feel free to give the latest build another try.

@grabmateusz
Copy link
Copy Markdown

Hi @edmundhung, newest package tested o both Next.js 15.5.14 and 16.1.6.
Looking forward for stable package with those changes!

@edmundhung edmundhung merged commit 75f5f0a into main Mar 26, 2026
7 checks passed
@edmundhung edmundhung deleted the edmundhung/r2-remote-cache-population-clean branch March 26, 2026 15:37
@edmundhung
Copy link
Copy Markdown
Contributor Author

Thanks for all the feedbacks, @grabmateusz. This is released in v1.18.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Can't upload R2 incremental cache - recurring 503 Service Unavailable

4 participants