From 99ef0c9638e149b4caa874bfe88949ebc165ac6a Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Tue, 7 Apr 2026 13:20:29 +0700 Subject: [PATCH] fix: add no-cache headers to REST API, include ai_summary in response LiteSpeed Cache was serving stale REST API responses with 0 articles, causing the React frontend to show "No articles" despite 50+ articles in the DB. - Add Cache-Control and X-LiteSpeed-Cache-Control: no-cache headers to /articles endpoint - Include ai_summary and thumbnail_local columns in the REST API query (needed by React frontend) --- includes/class-peptide-news-rest-api.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/includes/class-peptide-news-rest-api.php b/includes/class-peptide-news-rest-api.php index d907a2b..d3a657e 100644 --- a/includes/class-peptide-news-rest-api.php +++ b/includes/class-peptide-news-rest-api.php @@ -111,7 +111,7 @@ class Peptide_News_Rest_API { * @return true|WP_Error */ public function validate_date_format( $value, $request, $param ) { - if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $value ) ) { + if ( ! preg_match( '/^\\d{4}-\\d{2}-\\d{2}$/', $value ) ) { return new WP_Error( 'rest_invalid_date', sprintf( __( 'Invalid date format for %s. Expected Y-m-d.', 'peptide-news' ), $param ), @@ -123,6 +123,9 @@ class Peptide_News_Rest_API { /** * GET /articles + * + * Returns active articles with no-cache headers so that + * LiteSpeed / CDN layers always serve fresh data. */ public function get_articles( $request ) { global $wpdb; @@ -134,24 +137,30 @@ class Peptide_News_Rest_API { $articles = $wpdb->get_results( $wpdb->prepare( "SELECT id, source, source_url, title, excerpt, ai_summary, author, - published_at, categories, tags + thumbnail_url, thumbnail_local, published_at, categories, tags FROM {$table} WHERE is_active = 1 ORDER BY published_at DESC LIMIT %d OFFSET %d", - $count, - $offset + $count, + $offset ) ); - + $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE is_active = 1" ); - return rest_ensure_response( array( + $response = rest_ensure_response( array( 'articles' => $articles, 'total' => (int) $total, 'page' => $page, 'per_page' => $count, 'total_pages' => ceil( $total / $count ), ) ); + + // Prevent LiteSpeed and browser caches from serving stale article data. + $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0' ); + $response->header( 'X-LiteSpeed-Cache-Control', 'no-cache' ); + + return $response; } /**