tlovett1 / simple-cache

A simple caching plugin for WordPress.
https://wordpress.org/plugins/simple-cache/
129 stars 33 forks source link

REST API and headers support #121

Closed luisherranz closed 3 years ago

luisherranz commented 3 years ago

This PR adds support to properly cache the REST API.

It doesn't make sense to cache the REST API without returning the headers because they contain necessary information about pagination, so I've added support to restore the response headers as well.

Breaking changes

1. The REST API is not cached anymore by default

Right now the REST API is cached using just like any other request, using index.html files. Then, those REST API respones are returned by Simple Cache, but with a Content-Type of text/html instead of application/json.

This PR, as it is, stops caching the REST API endpoints. That is a breaking change. But I considered that the way Simple Cache is caching the REST API right now is not useful.

Anyway, if you consider Simple Cache should keep caching the REST API by default, it would be as simple as using a default of true in the new enable_rest_api_cache field. Code.

If we do that, it will still introduce a change, because it will start saving the responses as index.json (instead of index.html) and returning a Content-Type of application/json. But I think that could be considered as a fix.

New Settings

1. Cache the REST API

Present in advanced mode only. false by default.

It saves the REST API response in an index.json file, instead of the index.html file. The reason for this is that these requests need to be served as Content-Type: application/json so it'll be easier for people using Nginx or a static hosting service like Vercel or Netlify to distinguish between the index.html and the index.json. It also supports gzip.

Apart from that, I've added a couple of changes to the way these assets are cached:

2. Restore Headers

Present in advanced mode only. false by default.

It saves the response headers and sends them again when serving the cached asset. It works for both HTML and REST API assets. It saves the headers in a headers.json file.

Other changes

1. Don't flush the cache when the user edits a draft post

I've introduced a fix to avoid flushing the cache when the user is editing a draft post. Code. I don't know if you were doing so on purpose and it had other considerations, so I'd love to hear your opinion.

Also, this is a totally unrelated change, so if you prefer me to do this in another PR let me know.

2. Translations

I've updated both the main .pot file to reflect the code changes and the Spanish translation with complete translations, even from old texts.

3. New X-Simple-Cache header

I introduced a X-Simple-Cache header with a value of MISS or HIT so it's easier to spot the behavior of the plugin.

Again, I can revert it if you don't want to introduce this.


Finally, I'd like to add that if you want to keep Simple Cache as simple as possible, it could make sense not to add these features as new settings and just enable them by default, maybe in a new major version. Let me know what you think about that.

luisherranz commented 3 years ago

Travis is failing, but it seems unrelated to this PR:

You are using Composer 2, which some of your plugins seem to be incompatible with. Make sure you update your plugins or report a plugin-issue to ask them to support Composer 2.