- 🇺🇸United States luke adams
I just ran into this today and figured out a solution that seems to be working nicely.
In my case I extended the advagg JsOptimizer with my own service decorator and class as follows.
my_module.services.yml
services: my_module.js_optimizer: class: Drupal\my_module\Asset\AmpJsOptimizer public: false decorates: advagg.optimizer.js decoration_priority: 10 arguments: [ '@config.factory', '@event_dispatcher', '@cache.advagg', '@my_module.js_optimizer.inner' ]
/my_module/src/Asset/AmpJsOptimizer.php
<?php namespace Drupal\my_module\Asset; use Drupal\advagg\Asset\JsOptimizer; use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; /** * Extends the JsOptimizer class. */ class AmpJsOptimizer extends JsOptimizer { /** * {@inheritdoc} */ public function __construct(ConfigFactoryInterface $config_factory, ContainerAwareEventDispatcher $event_dispatcher, CacheBackendInterface $cache) { $this->extension = 'js'; parent::__construct($config_factory, $event_dispatcher, $cache); } /** * Override the convertPathProtoclRelative method from Advagg Optimizer. * * @param string $path * Path to check. * * @return string * The converted path or the original path if already protocol relative. */ protected function convertPathProtocolRelative($path) { if (str_contains($path, 'ampproject.org')) { return $path; } if (strpos($path, 'https://') === 0) { $path = substr($path, 6); } elseif (strpos($path, 'http://') === 0) { $path = substr($path, 5); } return $path; } }
This has let me override the `convertPathProtocolRelative()` method only (defined in `advagg/src/Asset/AssetOptimizer`), to add a check for ampproject URL, and if so, just return the path un-altered. I've checked that the rest of the methods being called from the parent Classes are all running as they should, and I'm seeing my ampproject URLs coming through with full protocols.
First time writing a decorator, so if something looks weird to anyone who's done this before, please let me know!