3

In order to work with Cloudflare's Rocket Loader service, certain scripts need to have their script tags updated with the cf-async="false" attribute like so:

<script type="text/javascript" cf-async="false">

Specifically, I need to add this attribute to the Drupal settings script tag, that gets added inline at the top of every page.

My first thought was to do this via hook_js_alter(), like:

function custom_helper_js_alter(&$javascript) {
  $javascript['settings']['cf-async'] = 'false';
}

But it doesn't seem to be possible to add custom attribute tags via this function. So my question is, is there any way at all to do what I want without hacking core?

1 Answer 1

5

There is, but it's not pretty. The markup produced by drupal_get_js() is pretty closed off; it uses theme_html_tag(), but what it provides for variables means identifying the Drupal.settings script is a pain.

This is about as good as you'll get. In custom module:

function MYMODULE_preprocess_html_tag(&$vars) {
  if ($vars['element']['#tag'] == 'script' && strpos($vars['element']['#value'], 'jQuery.extend(Drupal.settings') === 0) {
    $vars['element']['#attributes']['cf-async'] = 'false';
  }
}

As you rightly mentioned through the comments you could also attack it at the theme override level instead of the module level. In template.php:

function MYTHEME_preprocess_html_tag(&$variables) {
  if ($variables['element']['#tag'] == 'script' && strpos($variables['element']['#value'], 'jQuery.extend(Drupal.settings') === 0) {
    $variables['element']['#attributes']['cf-async'] = 'false';
  }
}

If you have modules that mess with JavaScript (e.g. LabJS), the string match will be slightly different.

5
  • Thanks Clive for the helpful answer. I think you mean MYTHEME_html_tag(&$vars) in template.php, though, rather than MYMODULE_preprocess_html()? Commented Mar 24, 2015 at 16:43
  • 1
    I edited the original answer with the code that is working for me. Please feel free to re-edit, or update if that solution is not what you were intending. Commented Mar 24, 2015 at 16:50
  • It wasn't what I intended @Jordan, but your code is a perfectly valid alternative. I've changed the answer to include both
    – Clive
    Commented Mar 24, 2015 at 16:54
  • I changed the code again... I think this should be _preprocess_html_tag() in both cases (module and theme)... tested both and working as expected. hook_preprocess_html() gives different $vars that does not work with this code. Commented Mar 24, 2015 at 17:04
  • By using above approach this add attribute cf-async after the src but to make it working we need to add before src as per documentation support.cloudflare.com/hc/en-us/articles/… (point number 2 ) Please suggest how we can add it before src ?
    – jas
    Commented Nov 25, 2016 at 12:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.