From 97f2b9b0893530ee0fb93387fbbff98e9444012b Mon Sep 17 00:00:00 2001 From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com> Date: Thu, 31 Oct 2024 08:15:51 +0000 Subject: [PATCH] Optimise CDN assets class and add env var to switch off. (#764) * Optimise CDN assets class and add env var to switch off. * Update amazon-s3-and-cloudfront-assets.php --- config/application.php | 3 +++ .../clarity/inc/amazon-s3-and-cloudfront-assets.php | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/application.php b/config/application.php index a42bcd34c..f718f78ac 100644 --- a/config/application.php +++ b/config/application.php @@ -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 */ diff --git a/public/app/themes/clarity/inc/amazon-s3-and-cloudfront-assets.php b/public/app/themes/clarity/inc/amazon-s3-and-cloudfront-assets.php index 4a12113d1..142609cd2 100644 --- a/public/app/themes/clarity/inc/amazon-s3-and-cloudfront-assets.php +++ b/public/app/themes/clarity/inc/amazon-s3-and-cloudfront-assets.php @@ -4,6 +4,7 @@ use Amazon_S3_And_CloudFront_Pro; use Exception; +use Roots\WPConfig\Config; /** * Amazon S3 and CloudFront - assets. @@ -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; @@ -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']; @@ -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; }