How to Cache wp-json with CloudFront

I’m not sure if I’m the only one who has tried to Cache WordPress API endpoint wp-json without a plugin, but I sure didn’t find anything useful out there.

I have been working with the WordPress API bringing some blog content into a non WordPress site and noticed that like most things with WordPress the API calls were terribly slow.

I went down the typical WordPress path of plugin exploration. Testing a few, Enabling Memcached, Enabling Object caching all without any real success.

Annoyed and convinced that the API is somehow treated totally differently from other WordPress queries ( is it? ). I decided to use CloudFront for no other reason than it’s what my company uses for SSL.

After adding a Behavior for /wp-json/* I set off to find the correct combination of caching that still allowed the API to work properly as it still uses URL rewriting, relies on some headers, and needs query strings.

My Basic settings look something like this.

Under Cache Based on Selected Requests choose Whitelist and chose the following.