We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29988
    • 89 Posts
    This is an experimental plugin for the upcoming caching plugin. When it’s hooked with OnWebPageInit, it saves the content of the generated page to /htcache/path/alias.html (so create /htcache and set right rights).

    function mkdir_r($dirName, $rights=0777){
       $dirs = explode('/', $dirName);
       $dir='';
       foreach ($dirs as $part) {
           $dir.=$part.'/';
           if (!is_dir($dir) && strlen($dir)>0)
               mkdir($dir, $rights);
       }
    }
    
    function repairpath($url) {
      return str_replace("//","/",$url);
    }
    
    function htcache_out($output) {
      $htcache_ignore = "file favicon.ico, file robots.txt";
      global $modx;
      $htcache_root="c:/root/htcache";
      $dirname="";
      if (count($_GET)<=1) {
      if (!isset($_GET["q"])) $filename=repairpath($htcache_root."/index.html"); else {
           if (strpos(" ".$htcache_ignore,"file ".$_GET["q"])) {
            $msg="Error: this file '".$_GET["q"]."' should exist and can't be renderred by the CMS.";
            $output=$msg;
            return $msg;
            }
          $filename=repairpath($htcache_root."/".$_GET["q"].".html");
        }
           if (!file_exists($filename)) {
            $dirname=preg_replace("|^(.*)/[^/]*$|","\\1",$filename);
            mkdir_r($dirname);
          }
    
      $fcached=fopen($filename,"w");
      fwrite($fcached,$output);
      fclose($fcached);
    
      }
      
      //$output=$filename."<br/><br/>".$output;
      return $output;
    }
    
    $e = &$modx->Event;
    
    switch ($e->name) {
    	case "OnWebPageInit":	
        ob_end_clean();
        $actual_ob_content=ob_get_contents();
        ob_end_clean();
        ob_start("htcache_out");
        echo $actual_ob_content;
        ob_start();
    /*
          $doc = &$modx->documentOutput;
          $modx->documentOutput=$modx->rewriteUrls($doc);
    */
    
        break;
    		
    default:	// stop here
    		return; 
    		break;	
    }
    return 1;


    You then add to .htaccess these lines:

    RewriteCond %{REQUEST_URI} ^/index\..{3,4}$
    RewriteCond %{QUERY_STRING} ^$ 
    RewriteCond htcache/index.html -F
    RewriteRule index.php htcache/index.html [L,QSA]
    
    RewriteCond htcache%{REQUEST_URI}.html -f
    RewriteRule ^(.*)$ htcache%{REQUEST_URI}.html [L,QSA]


    ...and all documents will be taken from the static .html files, not even running PHP.

    The only problem is, that no refreshing is implemented yet =)

    Please change the $htcache_root.

    Please appreciate my nice ob_stuff tweaking to rewrite the default handling ;-) :

    [tt] ob_end_clean();
    $actual_ob_content=ob_get_contents();
    ob_end_clean();
    ob_start("htcache_out");
    echo $actual_ob_content;
    ob_start();[/tt]

    It works =)
      • 32963
      • 1,732 Posts
      Very nice plugin tillda smiley I can se where this would come in handy for those pages that does not require much dynamic content.

      One question... will the rewrite rule work with non-cachaeble pages?
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 29988
        • 89 Posts
        Quote from: xwisdom at Jul 19, 2006, 12:43 PM

        One question... will the rewrite rule work with non-cachaeble pages?

        Not a rewrite rule. If a page is non-cacheable, the plugin must be told not to write the HTML file (not implemented yet). If it isn’t there, it won’t be used.
          • 4369
          • 31 Posts
          Hey Tilda,

          OpenGeek has draw my attention to this post, as I needed a solution for caching pages that make use of non-modx tables content.
          Was there any progress in the plugin development since this post has been posted ?
          Have you encounter any bugs with it ?


          Thank you. (and thank you, OpenGeek ;-)