Skip to content

Commit

Permalink
Website: Avoid edge-caching conditionally redirected resources (#1351)
Browse files Browse the repository at this point in the history
## What is this PR doing?

This PR disables edge caching for conditionally redirected resources.

## What problem is it solving?

The WP Cloud edge cache seems to avoid caching some redirects. But for
conditionally redirected resources like `/wordpress.html`, the edge
cache can cache the resource when not redirected, and after that, PHP is
no longer given a chance to conditionally redirect.

## How is the problem addressed?

If a resource has conditional redirects, we now explicitly disable
caching for that resource.

## Testing Instructions

- Tested manually on playground-dot-wordpress-dot-net.atomicsites.blog

Related to #1197
  • Loading branch information
brandonpayton authored Apr 30, 2024
1 parent c04bbc5 commit 5d0c20c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/playground/website-deployment/custom-redirects-lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function playground_file_needs_special_treatment( $path ) {
}

function playground_handle_request() {
$may_edge_cache = true;

// TODO: If needed, switch to a printf style signature
// so string interpolation only occurs when actually logging.
$log = defined( 'PLAYGROUND_DEBUG' ) && PLAYGROUND_DEBUG
? function ( $str ) { error_log( "PLAYGROUND: $str" ); }
: function () {};
Expand Down Expand Up @@ -45,6 +49,10 @@ function playground_handle_request() {
//
$redirect = playground_maybe_redirect( $requested_path );
if ( false !== $redirect ) {
// Disable edge caching because this resource may be redirected by PHP.
// Note: Using the header `Vary: Referer` does not seem to affect cacheability.
$may_edge_cache = false;

$should_redirect = true;
if ( isset( $redirect['condition']['referers'] ) ) {
$should_redirect = false;
Expand Down Expand Up @@ -128,9 +136,13 @@ function playground_handle_request() {
header( $custom_header );
}
} else {
$log( "Marking for cache: '$resolved_path'" );
header( 'A8C-Edge-Cache: cache' );
//header( 'Cache-Control: max-age=300, must-revalidate' );
if ( $may_edge_cache ) {
$log( "Marking for cache: '$resolved_path'" );
header( 'A8C-Edge-Cache: cache' );
} else {
$log( "Skipping edge cache: '$resolved_path'" );
header( 'Cache-Control: no-cache' );
}
}

if ( 'HEAD' === $_SERVER['REQUEST_METHOD'] ) {
Expand Down

0 comments on commit 5d0c20c

Please sign in to comment.