Drupal: CSS Aggregation im Private Mode

Hat man keine Kapazitätsprobleme, kann man mit diesem Kleinen Modul die CSS optimieren, vereinigen und Cachen. Leider ist dies ansonsten in Drupal 6 Core nicht möglich. Ich weiss, der Cache braucht relativ viel Platz in der DB, aber dies ist in meinem Fall kein Problem. Das Modul erstellt für jede Seite, welche eine andere CSS Dateien Konstellation hat, ein eigener Cache-Eintrag. Die Geschwindigkeit zum Laden der Seite ist bei mir extrem gestiegen (> 33%). Anregungen bitte im Kommentar posten!

  1. // die Folgenden Konstanten anpassen
  2. define('MINICSSDPATH', '/var/www/vhosts/yourhost/httpdocs');
  3. define('MINICSSCACHE', true);
  4.  
  5.  
  6. function minicss_menu() {
  7. $items = array();
  8. $items['minicss/css'] = array(
  9. 'title' => 'minicss css',
  10. 'page callback' => 'minicss_minify',
  11. 'page arguments' => array(2,'css'),
  12. 'access callback' => true,
  13. 'type' => MENU_CALLBACK,
  14. );
  15. return $items;
  16. }
  17.  
  18. /**
  19.  * Implementation of hook_preprocess_page()
  20.  */
  21. function minicss_preprocess_page(&$variables) {
  22. if (!empty($variables['styles'])) {
  23. preg_match_all('|href="(.*?\.css)|', $variables['styles'], $matches1);
  24. $variables['styles'] = '<link type="text/css" '.
  25. 'rel="stylesheet" media="all" '.
  26. 'href="/minicss/css/'.
  27. base64_encode(implode(',',$matches1[1])).'" />';
  28. }
  29. }
  30.  
  31. function minicss_minify($files,$type) {
  32. header('Content-Type: text/'.$type);
  33. header('Cache-control: public');
  34.  
  35. // The Checksum identifies the unique css-File-Package
  36. $checksum = md5($files);
  37. // Recive Cached CSS-Package
  38. $cache = cache_get('minicss_'.$checksum);
  39. // If Package does not exist, create it and output
  40. if (!$cache) {
  41. $output = '';
  42. $files = explode(',',base64_decode($files));
  43. foreach ($files as $file) {
  44. $content = file_get_contents(MINICSSDPATH.$file);
  45. // remove comments
  46. $content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content);
  47. //$content = str_replace("\t", ' ', $content); // Removing Tabs somehow creates problems...
  48. // Remove special chars
  49. $content = str_replace(array("\r\n", "\r", "\n", ' ', ' ', ' '), '', $content);
  50.  
  51. // urls sind relativ zum CSS file und müssen dementsprechend relativ zum miniCSS angepasst werden
  52. $path = str_replace(array_pop(explode('/',$file)), '', $file);
  53. $content = str_replace('url(', 'url('.$path,$content);
  54. $output .= $content;
  55. }
  56. // Write new created Package into Cache for next time
  57. if (MINICSSCACHE) cache_set('minicss_'.$checksum,$output);
  58. header ("Content-Length: " . strlen($output));
  59. print $output;
  60. }
  61. else {
  62. // If Cache-package already exists, output cache, also add Information about version
  63. $output = '/* Retrieved from Cache; Version: '.date('Y.m.d H:i',$cache->created).' */'."\n\n".$cache->data;
  64. header ("Content-Length: " . strlen($output));
  65. print($output);
  66. }
  67. }

Evtl. muss man in der Datenbank Tabelle "System" das Modul-weight noch an den Schluss setzen.

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <php>
  • Zeilen und Absätze werden automatisch erzeugt.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal>, <js>, <php>, <vb>. PHP-Quellcode kann in <?php ... ?> or <% ... %> eingeschlossen werden.
  • Verlinkte Bilder mit dem 'rel="lightbox"' in <a> Tags wird die Lighbox aufrufen, wenn darauf geklickt wurde.

Weitere Informationen über Formatierungsoptionen

Mit dem Absenden dieses Formulars, akzeptieren Sie die Datenschutzrichtlinie von Mollom.