diff --git a/README.md b/README.md index 7b36e95..4f8084c 100644 --- a/README.md +++ b/README.md @@ -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=`, 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 diff --git a/includes/Core/PageCache.php b/includes/Core/PageCache.php new file mode 100644 index 0000000..95dc711 --- /dev/null +++ b/includes/Core/PageCache.php @@ -0,0 +1,21 @@ +set_404(); diff --git a/includes/Rest/DonateController.php b/includes/Rest/DonateController.php index 13df41d..79c88db 100644 --- a/includes/Rest/DonateController.php +++ b/includes/Rest/DonateController.php @@ -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,9 +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' ); - // LiteSpeed Cache would otherwise hold REST responses for days; it has its own opt-out hook. - do_action( 'litespeed_control_set_nocache', 'streamgoals progress' ); - $res->header( 'X-LiteSpeed-Cache-Control', 'no-cache' ); + PageCache::skip( 'progress' ); // LiteSpeed would otherwise hold REST responses for days + $res->header( PageCache::LITESPEED_HEADER, 'no-cache' ); return $res; } @@ -171,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; } diff --git a/includes/Rest/OverlayController.php b/includes/Rest/OverlayController.php index 37c8c93..6ee029e 100644 --- a/includes/Rest/OverlayController.php +++ b/includes/Rest/OverlayController.php @@ -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 ) ); } diff --git a/tests/Unit/PageCacheOptOutTest.php b/tests/Unit/PageCacheOptOutTest.php new file mode 100644 index 0000000..4011c3e --- /dev/null +++ b/tests/Unit/PageCacheOptOutTest.php @@ -0,0 +1,29 @@ + "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(' ) ); + } +}