From 5649ba7b1267c1eaba7dc86168632bad81b84a80 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 16 Feb 2019 12:04:22 +0100 Subject: [PATCH] Short-circuit asset CDN in AMP responses --- modules/photon-cdn.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/photon-cdn.php b/modules/photon-cdn.php index 1945904b57cf0..d41af88e6d516 100644 --- a/modules/photon-cdn.php +++ b/modules/photon-cdn.php @@ -39,6 +39,17 @@ public static function go() { public static function cdnize_assets() { global $wp_scripts, $wp_styles, $wp_version; + /* + * Short-circuit if AMP since not relevant as custom JS is not allowed and CSS is inlined. + * Note that it is not suitable to use the jetpack_force_disable_site_accelerator filter for this + * because it will be applied before the wp action, which is the point at which the queried object + * is available and we know whether the response will be AMP or not. This is particularly important + * for AMP-first (native AMP) pages where there are no AMP-specific URLs. + */ + if ( Jetpack_AMP_Support::is_amp_request() ) { + return; + } + /** * Filters Jetpack CDN's Core version number and locale. Can be used to override the values * that Jetpack uses to retrieve assets. Expects the values to be returned in an array. @@ -92,6 +103,12 @@ public static function cdnize_assets() { * @return string The expected relative path for the CDN-ed URL. */ public static function fix_script_relative_path( $relative, $src ) { + + // Note relevant in AMP responses. See note above. + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $relative; + } + $strpos = strpos( $src, '/wp-includes/' ); // We only treat URLs that have wp-includes in them. Cases like language textdomains