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)
This commit is contained in:
@@ -111,7 +111,7 @@ class Peptide_News_Rest_API {
|
|||||||
* @return true|WP_Error
|
* @return true|WP_Error
|
||||||
*/
|
*/
|
||||||
public function validate_date_format( $value, $request, $param ) {
|
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(
|
return new WP_Error(
|
||||||
'rest_invalid_date',
|
'rest_invalid_date',
|
||||||
sprintf( __( 'Invalid date format for %s. Expected Y-m-d.', 'peptide-news' ), $param ),
|
sprintf( __( 'Invalid date format for %s. Expected Y-m-d.', 'peptide-news' ), $param ),
|
||||||
@@ -123,6 +123,9 @@ class Peptide_News_Rest_API {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /articles
|
* GET /articles
|
||||||
|
*
|
||||||
|
* Returns active articles with no-cache headers so that
|
||||||
|
* LiteSpeed / CDN layers always serve fresh data.
|
||||||
*/
|
*/
|
||||||
public function get_articles( $request ) {
|
public function get_articles( $request ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
@@ -134,24 +137,30 @@ class Peptide_News_Rest_API {
|
|||||||
|
|
||||||
$articles = $wpdb->get_results( $wpdb->prepare(
|
$articles = $wpdb->get_results( $wpdb->prepare(
|
||||||
"SELECT id, source, source_url, title, excerpt, ai_summary, author,
|
"SELECT id, source, source_url, title, excerpt, ai_summary, author,
|
||||||
published_at, categories, tags
|
thumbnail_url, thumbnail_local, published_at, categories, tags
|
||||||
FROM {$table}
|
FROM {$table}
|
||||||
WHERE is_active = 1
|
WHERE is_active = 1
|
||||||
ORDER BY published_at DESC
|
ORDER BY published_at DESC
|
||||||
LIMIT %d OFFSET %d",
|
LIMIT %d OFFSET %d",
|
||||||
$count,
|
$count,
|
||||||
$offset
|
$offset
|
||||||
) );
|
) );
|
||||||
|
|
||||||
$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE is_active = 1" );
|
$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE is_active = 1" );
|
||||||
|
|
||||||
return rest_ensure_response( array(
|
$response = rest_ensure_response( array(
|
||||||
'articles' => $articles,
|
'articles' => $articles,
|
||||||
'total' => (int) $total,
|
'total' => (int) $total,
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'per_page' => $count,
|
'per_page' => $count,
|
||||||
'total_pages' => ceil( $total / $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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user