Enable preload links for responsive hero images which have srcset and sizes attributes. This is disabled by default because only Chrome supports imagesrcset and imagesizes on preload links. This eliminates the Optimizer from raising this notice: CannotPreloadImage: Not preloading the hero image because of the presence of a srcset attribute…
<?php | |
/** | |
* AMP Responsive Hero Image Preload Link Opt-In plugin bootstrap. | |
* | |
* @package Google\AMP_Responsive_Hero_Image_Link_Opt_In | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2021 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Responsive Hero Image Preload Link Opt-In | |
* Plugin URI: https://gist.github.com/westonruter/6adb64e6e8c858a40ac1dc51b03f16d8 | |
* Description: Enable preload links for responsive hero images which have <code>srcset</code> and <code>sizes</code> attributes. This is disabled by default because only Chrome supports <code>imagesrcset</code> and <code>imagesizes</code> on preload links. This eliminates the Optimizer from raising this notice: “CannotPreloadImage: Not preloading the hero image because of the presence of a <code>srcset</code> attribute, which can currently only be preloaded by Chromium-based browsers (see <a href="https://web.dev/preload-responsive-images/">https://web.dev/preload-responsive-images/</a>).” | |
* Version: 0.1 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Gist Plugin URI: https://gist.github.com/westonruter/6adb64e6e8c858a40ac1dc51b03f16d8 | |
*/ | |
namespace Google\AMP_Responsive_Hero_Image_Link_Opt_In; | |
use AmpProject\Optimizer\Transformer\PreloadHeroImage; | |
use AmpProject\Optimizer\Configuration\PreloadHeroImageConfiguration; | |
add_filter( | |
'amp_optimizer_config', | |
function ( $config ) { | |
$config[ PreloadHeroImage::class ][ PreloadHeroImageConfiguration::PRELOAD_SRCSET ] = true; | |
return $config; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c