How to set up the REST API to refer to media on a CDN

I ran into the same issue today. Thank you for the question! I am implementing a ReactJS-App in a theme. So I enqueue my scripts and styles with the help of the corresponding WordPress hooks. I configured the w3 total cache plugin, so they’re getting served via the CloudFront CDN.

So far so good, it works, but when it comes to media files requested over the REST API (as being part of the richtext content attribute) my URLs are also not pointing to the CDN.

I solved it by using another plugin: https://de.wordpress.org/plugins/amazon-s3-and-cloudfront/

It rewrites the URLs from all files in the upload directory – also when they are requested over the REST API.

I am a bit concerned about using two plugins, although I configured the w3 total cache plugin to only serve theme files over CDN and the amazon cloudfront plugin to only serve media files. But it works…

What is your current solution?

Thanks and kind regards
Niels