Skip to content

Conversation

@agcty
Copy link

@agcty agcty commented Oct 18, 2025

Adds comprehensive custom domain configuration for Cloudflare R2 buckets with automatic DNS management, enabling production-ready content
delivery through custom domains.

Features

  • Custom Domain Configuration: New customDomain property on R2Bucket supporting:

    • Domain name and Cloudflare Zone ID
    • TLS version control (1.0, 1.1, 1.2, 1.3)
    • Custom cipher suite configuration
    • Enable/disable toggle
  • Automatic DNS Management:

    • Automatically creates proxied CNAME records pointing to public.r2.dev
    • Handles DNS record lifecycle (create, update, delete)
    • Detects and preserves read-only records created by Cloudflare dashboard
    • Cleans up DNS records when custom domain is removed
  • Full CRUD Support:

    • Create custom domains with automatic DNS setup
    • Update domain settings (TLS, ciphers, enabled state)
    • Handle domain name changes (deletes old, creates new)
    • Clean deletion of both custom domain and DNS records

Example Usage

import { R2Bucket } from "alchemy/cloudflare";

const cdnBucket = await R2Bucket("cdn-assets", {
  name: "cdn-assets",
  customDomain: {
    domain: "cdn.example.com",
    zone: "zone or zone-id",
    enabled: true,
    minTLS: "1.2"
  }
});

// DNS record is automatically created
console.log(cdnBucket.customDomain?.domain); // "cdn.example.com"

Test Coverage

-  Create custom domain with automatic DNS record
-  Verify DNS record configuration (CNAME, proxied)
-  Update custom domain settings (TLS, enabled state)
-  Remove custom domain with DNS cleanup
-  Handle read-only DNS records from dashboard

Documentation

- Added "With Custom Domain" section to R2 Bucket docs
- Includes examples and feature highlights

API References

- https://developers.cloudflare.com/api/resources/r2/subresources/custom_domains/
- https://developers.cloudflare.com/api/resources/dns/subresources/records/

Implements #1107 


Add comprehensive custom domain configuration for R2 buckets with automatic DNS management:

- Add `customDomain` property to `BucketProps` with domain, zoneId, enabled, minTLS, and ciphers configuration
- Automatically create and manage proxied CNAME DNS records pointing to public.r2.dev
- Handle DNS record lifecycle (create, update, delete) alongside custom domain operations
- Detect and preserve read-only DNS records created by Cloudflare dashboard
- Support TLS version and cipher suite customization
- Handle domain name changes by cleaning up old records and creating new ones
- Add comprehensive test coverage for custom domain CRUD operations
- Add documentation with examples for using custom domains

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
agcty and others added 3 commits October 19, 2025 20:58
… domains

When changing the custom domain for an R2 bucket (e.g., from test.domain.com
to test2.domain.com), Cloudflare sometimes creates read-only DNS records that
are managed automatically by the R2 service. Attempting to delete these
read-only records would fail with a "Bad Request" error.

This change improves the `deleteR2CustomDomainDnsRecord` function to:
- Check if the DNS record exists and whether it's read-only before deletion
- Silently skip deletion if the record is read-only (managed by Cloudflare)
- Only attempt deletion for records that are not read-only

This eliminates the warning message when changing custom domains and ensures
smooth transitions between different domain configurations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@coderabbitai
Copy link

coderabbitai bot commented Oct 20, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

2 participants