Disable "Convert absolute paths to be protocol relative paths." on AMP pages to avoid validation error

Created on 6 February 2021, almost 4 years ago
Updated 19 July 2024, 5 months ago

Problem/Motivation

AMP pages requires the full URL on scripts element. If you enable the option "Convert absolute paths to be protocol relative paths.", the paths are stripped like:

to

I propose to avoid the conversion on AMP pages (from AMP module: https://www.drupal.org/project/amp ) or, add a warning on the option description.

Thanks.

🐛 Bug report
Status

Needs review

Version

4.0

Component

Code

Created by

🇮🇹Italy finex

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸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!

Production build 0.71.5 2024