Compare commits

...
Author SHA1 Message Date
shadowdaoandClaude Sonnet 5.5 96bae1cbf9 Keep overlays and live responses out of LiteSpeed Cache
Test and Release / test (push) Successful in 3m47s
Test and Release / release (push) Successful in 13s
LiteSpeed Cache held overlay poll responses for 7 days, so OBS kept getting
304s and neither the goal bar nor alerts updated after a donation. The overlay
data, overlay pages and fee preview now opt out, as the progress route did.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 21:07:57 -08:00
shadowdaoandClaude Sonnet 5.5 0180aacd72 Progress route: opt out of LiteSpeed cache via its hook
Test and Release / test (push) Successful in 2m26s
Test and Release / release (push) Successful in 15s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXhhC8xipsBR7e3o76i
2026-11-16 08:37:10 -08:00
6 changed files with 61 additions and 1 deletions

No files matched your search

+3
View File
@@ -122,6 +122,9 @@ plus a counter); when it differs from the one the page was loaded with, the over
plugin does this automatically, and StreamGoals > Overlays > "Refresh all overlays now" does it on demand. Alert media
URLs also carry `?v=<file modified time>`, so replacing a file with the same name is picked up.
Overlay data, overlay pages, the progress bar and the fee preview are never page-cached (the plugin tells LiteSpeed
Cache to skip them). With another caching plugin or CDN, exclude `/wp-json/streamgoals/` and `/streamgoals-overlay/`.
## Updates
New versions appear in WordPress automatically (Dashboard > Updates and the Plugins screen), checked against
this repository's releases. The repository is public, so no token is needed. (If you run a private fork, paste an
+21
View File
@@ -0,0 +1,21 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Core;
/**
* Keeps live responses out of page caches. LiteSpeed Cache stores REST responses (and pages sending
* nocache_headers()) for days unless told otherwise, which freezes overlays that poll for changes.
*/
final class PageCache {
public const LITESPEED_HEADER = 'X-LiteSpeed-Cache-Control';
/** Call before any output, early enough that error responses are covered too. */
public static function skip( string $reason ): void {
do_action( 'litespeed_control_set_nocache', 'streamgoals ' . $reason );
if ( ! headers_sent() ) {
header( self::LITESPEED_HEADER . ': no-cache' ); // also covers LiteSpeed server caching without the plugin
}
}
}
+2
View File
@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace StreamGoals\Overlay;
use StreamGoals\Core\PageCache;
use StreamGoals\Rest\OverlayController;
/** Serves the transparent OBS browser-source page at /streamgoals-overlay/{token}/{widget}/. */
@@ -46,6 +47,7 @@ final class Router {
if ( '' === $token || '' === $widget ) {
return;
}
PageCache::skip( 'overlay page' ); // nocache_headers() alone does not stop LiteSpeed Cache
if ( ! is_string( $token ) || ! is_string( $widget ) || ! in_array( $widget, OverlayService::WIDGETS, true ) || ! OverlayController::verify_token( $token ) ) {
global $wp_query;
$wp_query->set_404();
+4 -1
View File
@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace StreamGoals\Rest;
use StreamGoals\Core\PageCache;
use StreamGoals\Core\GoalTargetPolicy;
use StreamGoals\Core\Money;
use StreamGoals\Core\Settings;
@@ -85,7 +86,8 @@ final class DonateController {
$res = rest_ensure_response( self::progress_for( $goal ) );
// Short shared cache lets a CDN collapse bursts; the page JS adds a cache-busting parameter after a donation.
$res->header( 'Cache-Control', 'public, max-age=10, s-maxage=10' );
$res->header( 'X-LiteSpeed-Cache-Control', 'no-cache' ); // LiteSpeed would otherwise hold REST responses for days
PageCache::skip( 'progress' ); // LiteSpeed would otherwise hold REST responses for days
$res->header( PageCache::LITESPEED_HEADER, 'no-cache' );
return $res;
}
@@ -169,6 +171,7 @@ final class DonateController {
}
public static function fee_preview( \WP_REST_Request $req ) {
PageCache::skip( 'fee preview' ); // follows the current fee settings and goal status
if ( $limited = self::limit( 'preview', 120, 60 ) ) {
return $limited;
}
+2
View File
@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace StreamGoals\Rest;
use StreamGoals\Core\PageCache;
use StreamGoals\Core\Settings;
use StreamGoals\Overlay\Build;
use StreamGoals\Overlay\OverlayService;
@@ -54,6 +55,7 @@ final class OverlayController {
}
public static function handle( \WP_REST_Request $req ): \WP_REST_Response|\WP_Error {
PageCache::skip( 'overlay data' ); // a cached poll response would freeze the goal bar and alerts
if ( ! self::verify_token( (string) $req['token'] ) ) {
return new \WP_Error( 'sg_not_found', 'Not found.', array( 'status' => 404 ) );
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
/** Live responses must opt out of page caching, or OBS overlays freeze behind LiteSpeed Cache. */
final class PageCacheOptOutTest extends TestCase {
public function test_every_live_response_opts_out_of_page_caching(): void {
$root = dirname( __DIR__, 2 ) . '/includes/';
$expected = array(
'Rest/OverlayController.php' => "PageCache::skip( 'overlay data' )",
'Overlay/Router.php' => "PageCache::skip( 'overlay page' )",
'Rest/DonateController.php' => "PageCache::skip( 'fee preview' )",
);
foreach ( $expected as $file => $call ) {
$this->assertStringContainsString( $call, (string) file_get_contents( $root . $file ), $file );
}
$helper = (string) file_get_contents( $root . 'Core/PageCache.php' );
$this->assertStringContainsString( 'litespeed_control_set_nocache', $helper );
}
public function test_overlay_opts_out_before_the_token_check_so_404s_are_not_cached(): void {
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Rest/OverlayController.php' );
$this->assertLessThan( strpos( $src, 'self::verify_token( (string)' ), strpos( $src, 'PageCache::skip(' ) );
}
}