Skip to content

Commit 1ae37c5

Browse files
donnchawpmatticbot
authored andcommitted
WPSC: Check that there is cached content to display in browser (#40342)
Check that there is cached content to display in browser Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12121105781 Upstream-Ref: Automattic/jetpack@adabdc7
1 parent 961ab35 commit 1ae37c5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is an alpha version! The changes listed here are not final.
2222
- General: Update minimum WordPress version to 6.6.
2323

2424
### Fixed
25+
- Caching: make sure there is cache content to serve, even if the cache file was found
2526
- Lossless image optimization for images (should improve performance with no visible changes).
2627
- Move trailing space out of i18n message.
2728
- Revert recent SVG image optimizations.

wp-cache-phase2.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,44 @@ function wp_cache_serve_cache_file() {
227227
if ( $wp_cache_gzip_encoding ) {
228228
if ( file_exists( $file . '.gz' ) ) {
229229
$cachefiledata = file_get_contents( $file . '.gz' );
230+
231+
if ( false === $cachefiledata ) {
232+
wp_cache_debug( 'The cached gzip file could not be read. Must generate a new one.' );
233+
return false;
234+
}
235+
230236
wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" );
231237
} else {
232-
$cachefiledata = gzencode( file_get_contents( $file ), 6, FORCE_GZIP );
238+
$cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
239+
240+
if ( false === $cachefiledata ) {
241+
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
242+
return false;
243+
}
244+
245+
$cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP );
233246
wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" );
234247
}
235248
} else {
236249
$cachefiledata = file_get_contents( $file );
250+
251+
if ( false === $cachefiledata ) {
252+
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
253+
return false;
254+
}
255+
237256
wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" );
238257
}
239258
} else {
240259
// get dynamic data from filtered file
241-
$cachefiledata = do_cacheaction( 'wpsc_cachedata', file_get_contents( $file ) );
260+
$cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
261+
262+
if ( false === $cachefiledata ) {
263+
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
264+
return false;
265+
}
266+
267+
$cachefiledata = do_cacheaction( 'wpsc_cachedata', $cachefiledata );
242268
if ( $wp_cache_gzip_encoding ) {
243269
$cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP );
244270
wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" );
@@ -301,6 +327,7 @@ function wp_cache_serve_cache_file() {
301327
}
302328
header( 'Last-Modified: ' . $local_mod_time );
303329
}
330+
304331
echo $cachefiledata;
305332
exit();
306333
} else {

0 commit comments

Comments
 (0)