Wp-rocket: API REST Cache return HTML instead of JSON

Created on 15 Apr 2020  Â·  10Comments  Â·  Source: wp-media/wp-rocket

When API REST caching is activated (with add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' );), API REST endpoints return cached data with HTML header instead of JSON header.

community [M] cache major bug

Most helpful comment

Hello,
Is there any news about this bug?

All 10 comments

Hi @hippocampestudio 🤚

To be able to work on this issue, we need first to replicate it.

To help us, thanks to provide us the exact steps to reproduce the issue by using this template:
https://github.com/wp-media/wp-rocket/issues/new?template=bug_report.md

Describe the bug
When API REST caching is activated (with add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' );), API REST endpoints return cached data with HTML header instead of JSON header.

To Reproduce
Steps to reproduce the behavior:

  1. Desactivate WPRocket
  2. Add add_filter( 'rocket_cache_reject_wp_rest_api', '__return_false' ); to functions.php
  3. Re-activate WP-rocket
  4. Go to /wp-json/wp/v2/posts (version with no cache)
  5. Refresh /wp-json/wp/v2/posts (cached version)

Expected behavior
The cached JSON should be a valid JSON but is HTML

Screenshots
https://i.ibb.co/0VJmstK/Capture-d-cran-2020-04-17-15-41-08.png

Duplicate of #291

@Tabrisrp Thanks for the check. I'm going to close the #291 as this one as the steps to reproduce.

Reproduce the issue ✅
Identified the issue on localhost.

Identify the root cause ✅
The root cause is that the JSON is saved as .html and no JSON headers are provided.

Scope a solution ✅
@hellofromtonya , @Tabrisrp ideas?
Basically this will affect the .htaccess rules and also the serve_cache_file()
https://github.com/wp-media/wp-rocket/blob/57003cff362df710d36acb49085115ea4d09e0eb/inc/classes/Buffer/class-cache.php#L167

A solution can be to save the file as .json instead of .html
Another solution is to provide the JSON headers.
Both solutions should be applied IF the filepath contains: wp-json/wp/v2/

Effort ✅
I think this is an [M], but probably I am overestimating it 😄

@Tabrisrp What do you think?

Saving the file with the json extension seems the better choice to me.

I'm not sure we should bother too much with the htaccess rules, as the speed benefit is pretty low, and a JSON file is relatively small anyway.

What we need to take into account there is the following:

  • Touching the cache class means moving it to the new architecture
  • It could be a good idea to check if we can make a subscriber dedicated to the WP API caching (and exclusion)
  • The tests will have to be handled a different way because the cache process is a bit different than the usual stuff

Hello,
Is there any news about this bug?

This issue is nearly 8 months old and still no solution :(

Any updates on this?

More and more websites are moving to use WordPress as a Headless CMS and caching these API endpoints is critical.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webtrainingwheels picture webtrainingwheels  Â·  5Comments

piotrbak picture piotrbak  Â·  4Comments

Tabrisrp picture Tabrisrp  Â·  5Comments

piotrbak picture piotrbak  Â·  3Comments

leurdo picture leurdo  Â·  4Comments