Score:0

Add SRI hash to existing CDN links

cn flag

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?

ru flag
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](https://www.drupal.org/docs/theming-drupal/adding-assets-css-js-to-a-drupal-theme-via-librariesyml#s-adding-attributes-to-script-elements)
ru flag
Better example link: https://www.drupal.org/docs/theming-drupal/adding-assets-css-js-to-a-drupal-theme-via-librariesyml#bootstrap
user3099225 avatar
cn flag
@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.
id flag
We don’t know what you did because you haven’t shown us the code or the error message.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.