Skip to content

Commit

Permalink
Optimise CDN assets class and add env var to switch off. (#764)
Browse files Browse the repository at this point in the history
* Optimise CDN assets class and add env var to switch off.

* Update amazon-s3-and-cloudfront-assets.php
  • Loading branch information
EarthlingDavey authored Oct 31, 2024
1 parent 0edbb3a commit 97f2b9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@
// Enable "agency" mode, which hides all external links and support resources.
Config::define('EWWWIO_WHITELABEL', true);

// Disable rewrite of enqueued assets to CDN.
Config::define('DISABLE_CDN_ASSETS', env('DISABLE_CDN_ASSETS'));

/**
* Debugging Settings
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amazon_S3_And_CloudFront_Pro;
use Exception;
use Roots\WPConfig\Config;

/**
* Amazon S3 and CloudFront - assets.
Expand All @@ -15,6 +16,7 @@ class AmazonS3AndCloudFrontAssets
{
private string $image_tag = '';
private string $transient_key;
private string $home_host;
private string $summary_file = 'build/manifests/summary.jsonl';
private bool $use_cloudfront_for_assets = false;

Expand All @@ -30,10 +32,16 @@ public function __construct()
return;
}

if (Config::get('DISABLE_CDN_ASSETS')) {
return;
}

// Get the first 8 chars only.
$this->image_tag = substr($_ENV['IMAGE_TAG'], 0, 8);
// Set the transient key - for caching the result of `checkManifestsSummary()`.
$this->transient_key = "cloudfront_assets_$this->image_tag";
// Get the home host.
$this->home_host = parse_url(Config::get('WP_HOME'), PHP_URL_HOST);

// Get the CloudFront host.
$this->cloudfront_host = $_ENV['AWS_CLOUDFRONT_HOST'];
Expand Down Expand Up @@ -189,7 +197,7 @@ public function rewriteSrc(string $src, string $handle): string
}

// If the host is not the same as WP_HOME, then return early.
if (parse_url($src, PHP_URL_HOST) !== parse_url(get_home_url(), PHP_URL_HOST)) {
if (parse_url($src, PHP_URL_HOST) !== $this->home_host) {
return $src;
}

Expand Down

0 comments on commit 97f2b9b

Please sign in to comment.