Delivering Magento Static Assets through Cloudflare R2 or AWS S3

Magento 2 stores and serves static assets—such as JavaScript, CSS, fonts, and other frontend files—locally by default. While this setup may suffice for small stores, it often becomes a bottleneck in high-traffic or globally distributed environments.

By offloading these static assets to cloud-based object storage like AWS S3 or Cloudflare R2 and delivering them through a CDN, you can dramatically improve page load speed, reduce server load, and streamline your deployment process.

Why Offload Static Content in Magento?

Magento’s default behavior compiles and caches static content in the pub/static directory on the local web server. This approach can introduce several issues:

  • High storage overhead, especially during deployments and versioning
  • Performance bottlenecks, as many users access static resources simultaneously
  • Slower delivery for global users, since assets are served from a single origin
  • Complex multi-node deployments, requiring manual synchronization of static files

By pushing static assets to S3-compatible storage and pairing them with a CDN, you can offload frontend delivery from your Magento backend and reduce infrastructure complexity.

Serving Static Content via AWS S3

  1. Create an S3 Bucket

    Open the AWS Console → Navigate to S3 → Create Bucket

    • Provide a globally unique bucket name
    • Choose your desired region
    • Disable "Block Public Access" if serving directly from the bucket (or use a CDN for control)
  2. Deploy Static Content to the Bucket

    Generate Magento's static assets

    bin/magento setup:static-content:deploy

    Upload the contents of the pub/static directory (excluding .htaccess and .gitignore) to your S3 bucket:

     aws s3 sync pub/static s3://magento-static-assets/static --acl public-read

    (Consider automating this step in your CI/CD pipeline for consistency.)

  3. Disable Static File Signing

    Magento appends version query strings (like ?version=xyz) to static files by default. This breaks CDN caching and causes 404 errors when using object storage. Disable this behavior with

    bin/magento config:set dev/static/sign 0
    bin/magento cache:flush

    This ensures that static file URLs remain clean and cache-friendly across deployments.

  4. Configure Static Base URLs in Magento

    Point Magento to use your external S3 or CDN URL for static content:

    bin/magento config:set web/unsecure/base_static_url https://<your-cdn-or-bucket-url>/
    bin/magento config:set web/secure/base_static_url https://<your-cdn-or-bucket-url>/
    bin/magento cache:flush

    Magento will now generate frontend links pointing to your S3 or CDN path.

Optional: Optimize with CloudFront

  • Go to AWS CloudFront → Create Distribution
  • Set the origin to your S3 bucket
  • Enable compression, HTTPS, and browser caching
  • Use the CloudFront URL as your static base URL in Magento

Alternative: Hosting Static Content with Cloudflare R2

Cloudflare R2 is a cost-effective, S3-compatible object storage solution—ideal for Magento stores with global traffic.

Why Choose Cloudflare R2?

  • Zero egress fees — major cost savings for high-volume stores
  • S3-compatible API — migrate with minimal changes
  • Built-in CDN — global asset delivery at lightning speed

Steps to Serve Static Assets from R2

  1. Create a Cloudflare R2 bucket
  2. Upload static files using AWS CLI or rclone (the AWS CLI (aws s3 sync), or any S3-compatible API to upload the contents of the pub/static directory to your Cloudflare R2 bucket.)
  3. Set Magento’s base static URL to point to your Cloudflare endpoint

Advantages of Offloading Magento Static Assets

  • Improved Load Times: CDN caching and global delivery reduce latency and TTFB (Time to First Byte).
  • Lower Server Load: Magento servers are freed from serving static content, focusing only on dynamic requests.
  • Streamlined CI/CD: No need to replicate static files across nodes in clustered environments.
  • Enhanced Scalability: Decoupled architecture allows for easier scaling and deployment in cloud environments.

Conclusion

Whether you go with AWS S3 + CloudFront or Cloudflare R2 + CDN, offloading Magento static assets is a best practice for high-performance, modern eCommerce. It ensures:

  • Faster page rendering for users worldwide
  • Reduced backend complexity
  • More agile deployments