0

I'm wondering how to disable css caching when you use the function drupal_add_css. In hook_preprocess I have some dynamic css loading with this function and by default it generates a cached version of the css file (f.e. style.css?ow65jm) and I couldn't find any information on how to disable this. It gives an issue with some third party applications how rebuild a page view (f.e. Hotjar) who expect a single version of an certain file.

I've checked the performance settings (admin/config/development/performance) and CSS and JS merge/compression is disabled.

Adding the CSS files directly into the tpl files can fixes this but can this also be accomplished with in the preprocess hook and with the drupal_add_css function.

Update - Added template.php and .tpl code:

template.php:

function mytheme_preprocess(&$vars, $hook) {
...
if (!(path_is_admin(current_path())) || (arg(0) == 'user' && arg(2) == 'edit')) {
      drupal_add_css('https://fonts.googleapis.com/css?family=Merriweather:700', array('type' => 'external'));
      drupal_add_css( $path_odisee . '/css/normalize.css', array('group' => CSS_THEME, 'every_page' => TRUE, 'weight' => -10));
      drupal_add_css( $path_odisee . '/css/odisee.css', array('group' => CSS_THEME, 'every_page' => TRUE, 'weight' => 0));
      drupal_add_css( $path_odisee . '/css/override.css', array('group' => CSS_THEME, 'every_page' => TRUE, 'weight' => 0));
    }
...
}

html.tpl.php:

...
<head>
  <?php print $head; ?>
  <title><?php print $head_title; ?></title>
  <meta name="viewport" content="width=device-width,initial-scale=1">

  <?php print $styles; ?> <!-- here the preprocessed CSS gets loaded -->

</head>
...

Additional information: The people over at Hotjar suggested to keep the cached versions of the css files on the server. Any suggestion on how to force this?

  • Can you provide your current code for drupal_add_css as well as the code that you put in the tpl file? – mikeytown2 Sep 12 '17 at 16:46
  • I'm still confused as to what you're trying to accomplish. Can you define what you mean when you say "css caching"? Are you using adaptivetheme? I know adaptivetheme will put mutiple css files into $theme_name . '.responsive.styles.css'. From the theme's code: You can disable responsive capability using the theme settings - look under the Global settings. – mikeytown2 Sep 13 '17 at 16:45
  • The problem is mainly temporary css files which is created (styles.css?ow65jm) every time the cache is cleared a new version of styles.css gets created and the analytics tool (Hotjar) i use can't rebuild the page because of the absence of the file. So there are two options preventing the cached css and force the static file or finding a way to save the 'old' versions of the styles. – kim.kaho Sep 15 '17 at 7:22
  • The creation of styles.css is most likely something related to the theme you're using; not apart of Drupal core and thus a lot harder to solve. – mikeytown2 Sep 15 '17 at 10:09
0

You can try using AdvAgg. Not sure it'll fix your issue because the source of the problem hasn't been identified. https://www.drupal.org/project/advagg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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