How to Cache wp-json with CloudFront

Categories: General

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.

Accept
Accept-Charset
Accept-Datetime
Accept-Encoding
Accept-Language
CloudFront-Forwarded-Proto
Host
Origin
Referer

While it is possible that I have more than what is needed, this allows any WordPress CloudFrott plugins to detect and handle HTTPS properly for you.

Next, I chose Object Caching and changed it to Customize choosing between 5 and 15 min for my TTL.

I disabled Forward Cookies to None and set Query String Forwarding and Caching to Forward all.

Last I set Compress Objects Automatically to Yes.

This dropped my terrible load times from 700ms to around 45ms.

Let me know how this worked for you and what you use for your caching strategy.

Cheers!


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!