lscache_wp icon indicating copy to clipboard operation
lscache_wp copied to clipboard

LiteSpeed HTML comment appended to response breaks JSON

Open westonruter opened this issue 5 years ago • 4 comments

The WordPress AMP plugin generates JSON responses when performing AMP validation. LiteSpeed Cache is currently appending this HTML comment to all responses, including those which do not have a text/html content type. This is breaking the AMP plugin's ability to obtain validation results. I've had to provide users with a workaround plugin to disable the litespeed_comment filter.

This logic here:

https://github.com/litespeedtech/lscache_wp/blob/6f3e5bd70db78e1900a8ba0c68765c7257534f5c/litespeed-cache/inc/litespeed-cache.class.php#L556-L558

The condition should be changed as follows:

$content_type = ini_get( 'default_mimetype' );
foreach ( headers_list() as $header ) {
	list( $name, $value ) = explode( ':', $header, 2 );
	if ( 'content-type' === strtolower( $name ) ) {
		$content_type = trim( $value );
		break;
	}
}
if ( $this->footer_comment && 'text/html' === substr( $content_type, 0, 9 ) ) {

Support forum topics:

  • https://wordpress.org/support/topic/amp-not-working-with-litespeed/#post-12716407
  • https://wordpress.org/support/topic/url-validation-failed-due-to-unexpected-json-in-amp-validation-response-2/

westonruter avatar Apr 22 '20 20:04 westonruter

Any updates on this one, we are still getting the lot of support issues

Here is a sample response

{
    "http_status_code": 200,
    "php_fatal_error": false,
    "results": [],
    "queried_object": {
        "id": 1959,
        "type": "post"
    },
    "url": "https://amp-support.rt.gw/my-account/?amp=1",
    "revalidated": true,
    "validated_url_post": {
        "id": 2012,
        "edit_link": "https://amp-support.rt.gw/wp-admin/post.php?post=2012&action=edit"
    }
}<!-- Page supported by LiteSpeed Cache 6.5.4 on 2025-01-03 15:30:19 -->

The condition that you have added to detect JSON and Ajax requests is not working properly on some sites.

https://github.com/litespeedtech/lscache_wp/blob/d3e8158950e95a561aa4b64827714571b7e80614/src/core.cls.php#L504-L513

milindmore22 avatar Jan 03 '25 15:01 milindmore22

Is add_filter('litespeed_comment', '__return_false'); possible?

szepeviktor avatar Jan 03 '25 16:01 szepeviktor

Yes, but this is a general issue that could impact any site that returns JSON instead of HTML.

westonruter avatar Jan 06 '25 19:01 westonruter

In any case, I've filed an issue to work around this: https://github.com/ampproject/amp-wp/issues/7967

westonruter avatar Jan 06 '25 19:01 westonruter