We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5091
    • 332 Posts
    chinesedream Reply #1, 17 years ago
    I am curious if MODx can do something like the way Magento handles it, that it groups and compresses all js files through a proxy script. Or maybe there is a snippet for this purpose already that I couldn’t find from search.

      • 15082
      • 195 Posts
      Don’t know anything about an "out of the box" solution.
      But... I’ve done this myself like that:

      - Chunk-Category for the JSs to include
      - Document that has is set to plain text an content type javascript (for me called [tt]combined.js[/tt]) with the following call inside:
      [[chunkListParser?chunks=`[[getChunks? &category=`js` ]]` &minify=`true` ]]


      - Snippet [tt]chunkListParser[/tt] patched as follows:
      <?php
      $minify = ( isset($minify) && $minify == 'true' ) ? true : false;
      
      $ar_chunks = split(',',$chunks);
      $ar_chunks = array_unique($ar_chunks);
      
      $output = '';
      
      foreach ($ar_chunks as $chunk) {
      	if ($chunk != '') {
      		$c = $modx->getChunk($chunk);
      		if ($c) $output .= $c . "\r\n";
      	}
      }
      
      if ($output != '' && isset($wrapperdiv)) {
      	$output = '<div id="'.$wrapperdiv.'">'.$output.'</div>';
      }
      if ($minify == true) {
      	require 'assets/snippets/jsmin/jsmin-1.1.1.php';
      	$output =  JSMin::minify($output);
      }
      
      return $output;
      ?>

      Original was taken from here http://wiki.modxcms.com/index.php/Create_TV-Based_Chunks

      - Snippet [tt]getChunks[/tt] patched as follows:
      <?php
      $q_chunks = $modx->db->query('
      	SELECT
      		chunk.*
      	FROM '.$modx->getFullTableName('site_htmlsnippets').' AS chunk
      		INNER JOIN '.$modx->getFullTableName('categories').' AS category
      		ON chunk.category = category.id
      	WHERE
      		category.category IN ("'.$category.'")
      	ORDER BY
      		name
      ');	
      
      $ar_chunks = array();
      while ($row = $modx->db->getRow($q_chunks)) {
      	$ar_chunks[] = $row['name'];
      }
      
      $l_chunks = implode(",",$ar_chunks);  //splitter character changed from "||" to "," by jusi
      return $l_chunks;
      ?>


      - jsmin from here http://code.google.com/p/jsmin-php/
      EDIT: path to jsmin is in [tt]chunkListParser[/tt] Snippet.

      EDIT2: re-reading your post, you’re talking about files... My solution takes the JSs from the DB.

      Hope this helps...

      Greetz
      JUSI
        • 15175
        • 116 Posts
        It maybe worthwhile having a look at minify, if you are not aware of it:

        http://code.google.com/p/minify/

        It combines CSS files as well, and serves everything gzipped.
          • 5091
          • 332 Posts
          chinesedream Reply #4, 17 years ago
          Thanks both.

          My server is hosted with Hostgator and it does not seemed allowing jsmin. I tested it a couple of times and it broke the sites I was developed, hostgator support couldn’t help either.

          The MODx site I am working right now, is hosted with Godaddy (alas!) which I have not yet tested if jsmin can work.