right now Autoptimize invalidate cache plugins
-
Hi,First, i love this plugin, it’s the best to optimize the scripts in the page, and i’ve tried all of them. With a caching plugins it works very very much better than w3 Total Cache. But…
Because the plugin autoptimize creates so many different name files for the cached scripts and styles it conflicts with plugins like supercache or w3total cache.
Because it has to make the file almost everytime the page is accessed the advantage of caching the files is lost.
i saw in the code that the author is using the content from the scripts to create the hash for the file and using md5 for that.Md5() is broken, i don’t know why but it creates new hashes with the same string over time. I could see this in a couple of plugins i’m developing that uses hashes as well to cache the results. I would use Sha1() that works better and is a little bit faster.
Also i’ve seen the plugin makes the hashes from the files content. I would use the file url in case of external js and the content when inline. Doing this changes for the javascript minfier i could get the load from 2.3 – 5.7 seconds (the second one is when it has to recreate the cache file, that actually is random) to 0.9 to 1.2 seconds. We are talking about a page 1.23MB and has a lot of scripts.
Ok now, i can show you an example, mi site is this one:
https://www.arturoemilio.es (i use it to develop live in it also, so if it’s down or weird looking just come back later)This is the speed i could get randomly (usually like 1.6 to 2 seconds) when autoptimize decided it hadn’t cached the page before.
https://gtmetrix.com/reports/www.arturoemilio.es/V1nIxpWkNow this is my actual speeds (because autoptimize knows is has cached already the files)
https://gtmetrix.com/reports/www.arturoemilio.es/jmutAWKGI’d like to ask the developer to add those changes to the plugin to fix the cache not being cached. This is the file modified:
private $namejs = ''; //Reads the page and collects script tags public function read($options) { //Remove everything that's not the header if($options['justhead'] == true) { $content = explode('</head>',$this->content,2); $this->content = $content[0].'</head>'; $this->restofcontent = $content[1]; } $excludeJS = $options['js_exclude']; $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS ); if ($excludeJS!=="") { $exclJSArr = array_filter(array_map('trim',explode(",",$excludeJS))); $this->dontmove = array_merge($exclJSArr,$this->dontmove); } $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); //Should we add try-catch? if($options['trycatch'] == true) $this->trycatch = true; // force js in head? if($options['forcehead'] == true) $this->forcehead = true; // get cdn url $this->cdn_url = $options['cdn_url']; // noptimize me $this->content = $this->hide_noptimize($this->content); // Save IE hacks $this->content = $this->hide_iehacks($this->content); // comments $this->content = $this->hide_comments($this->content); unset($namejs); //Get script files if(preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) { foreach($matches[0] as $tag) { if(preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$source)) { //External script $url = current(explode('?',$source[2],2)); $namejs .= $url; $path = $this->getpath($url); if($path !== false && preg_match('#\.js$#',$path)) { //Inline if($this->ismergeable($tag)) { //We can merge it $this->scripts[] = $path; } else { //No merge, but maybe we can move it if($this->ismovable($tag)) { //Yeah, move it if($this->movetolast($tag)) { $this->move['last'][] = $tag; } else { $this->move['first'][] = $tag; } } else { //We shouldn't touch this $tag = ''; } } } else { //External script (example: google analytics) //OR Script is dynamic (.php etc) if($this->ismovable($tag)) { if($this->movetolast($tag)) { $this->move['last'][] = $tag; } else { $this->move['first'][] = $tag; } } else { //We shouldn't touch this $tag = ''; } } } else { // Inline script // unhide comments, as javascript may be wrapped in comment-tags for old times' sake $tag = $this->restore_comments($tag); if($this->ismergeable($tag)) { preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code); $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]); $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code); $this->scripts[] = 'INLINE;'.$code; $namejs .= $code; } else { //Can we move this? if($this->ismovable($tag)) { if($this->movetolast($tag)) { $this->move['last'][] = $tag; } else { $this->move['first'][] = $tag; } } else { //We shouldn't touch this $tag = ''; } } // re-hide comments to be able to do the removal based on tag from $this->content $tag = $this->hide_comments($tag); } //Remove the original script tag $this->content = str_replace($tag,'',$this->content); } return true; } // No script files, great ;-) return false; } //Joins and optimizes JS public function minify() { foreach($this->scripts as $script) { if(preg_match('#^INLINE;#',$script)) { //Inline script $script = preg_replace('#^INLINE;#','',$script); //Add try-catch? if($this->trycatch) { $script = 'try{'.$script.'}catch(e){}'; } $this->jscode .= "\n".$script; } else { //External script if($script !== false && file_exists($script) && is_readable($script)) { $script = file_get_contents($script); $script = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$script); //Add try-catch? if($this->trycatch) { $script = 'try{'.$script.'}catch(e){}'; } $this->jscode .= "\n".$script; }/*else{ //Couldn't read JS. Maybe getpath isn't working? }*/ } } //Check for already-minified code //$this->md5hash = md5($this->jscode); $this->md5hash = sha1($namejs);
Also in my opinion i would change the preg_match and in general all the regex for DOM and Xpath. It’s much faster with complex html. I’ve tried but i get blank pages, i guess because of class and me not knowing what is in the variables.
- The topic ‘right now Autoptimize invalidate cache plugins’ is closed to new replies.