0

I have a Drupal site where there are few CDN links. Iwas thinking of adding SRI hashes to those links.

How can I do that?

Every solution I tried requires to add a custom module, but I don't know how these things work.

Somehow, I managed to create a custom module which creates a Hello page. I just followed a tutorial. Can I add hashes in it?

sri.info.yml

name: SRI Hash
type: module
description: 'SRI Hash'
package: custom
version: 8.x
core: 8.x

sri.routing.yml

sri.scripthash:
  path: '/sri_hash'
  defaults:
    _controller: '\Drupal\sri\Controller\SriController::scripthash'
    _title: 'SRI Hash'
    requirements: 
      _permission: 'access content'

SriController.php

namespace Drupal\sri\Controller;

class SriController {
  /**
   * Returns a simple page.
   *
   * @return array
   *   A simple renderable array.
   */
  public function scripthash() {
    $element = array(
      '#markup' => 'This is a page !',
    );
    return $element;
  }
}

Is there another way to do it?

4
  • 3
    I think you are overcomplicating this :-) Usually external assets are defined in *.libraries.yml files of your theme/module, and you can add the SRI hash in the attributes section of the asset, see example
    – Hudri
    Jun 2, 2022 at 8:18
  • 2
    Better example link: drupal.org/docs/theming-drupal/…
    – Hudri
    Jun 2, 2022 at 8:23
  • @Hudri thanks ... i have added the integrity hash, but after adding site breaks down,, shows message website encountered an error ! I have cleared the cache also, but it still shows the error, site works if i remove integrity hash. Jun 3, 2022 at 7:32
  • We don’t know what you did because you haven’t shown us the code or the error message.
    – cilefen
    Jun 3, 2022 at 10:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.