1

Due to big time lags because of a remote database, I am working on on a PHP/cron script fetching webpages from the CMS and saving them as static webpages. Then serving the static webpages to visitors. I have also decided to try and minify the resultant html file before saving it. All the above works pretty well (fetch, remove comments, minify, save), BUT swipe.js stops working.

My basic code is:

  // get the webpage from CMS
$contents = file_get_contents($cms_url);

  // remove comments
$contents = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/Uis', '', $contents);

  // minify
$contents = preg_replace('/^\s+|\n|\r|\s+$/m', '', $contents);  

  // delete old cached copy
unlink($web_url);

  // save new copy
file_put_contents($web_url , $contents);

After a lot of experimenting, I have come to the conclusion that swipe.js issue begins after removing the newline character (\n).

Has anyone dealt with this issue before? Is it a jQuery issue with the way they look for children as that seems to be the way swipe determines the correct divs? Or is it a swipe.js issue? Or is it the way I am removing the newline character? Or??

additional notes ----

Did some more experimenting with the minifaction aspect by using third party html minification tools and the same issue occurs with those minified files. So my PHP code is not the contributing factor.

It seems to be an issue with either jQuery or Swipe.js, as once the new line characters are removed the targeted content wrapper div isn't being modified.

Swipe.js is in an external file so is not be modified by my minifaction on the html files.

None of the other jQuery functions on the page are being effected by the minification.

Tom
  • 2,928
  • 4
  • 28
  • 36
  • 1
    Instead of using regex, I would suggest you to use a parser. Do not reinvent the wheel. – Jashwant May 04 '14 at 06:45
  • Any suggestions for a parser to try? – Tom May 04 '14 at 06:49
  • A quick google search gives me [this](http://verens.com/2008/05/20/efficient-js-minification-using-php/) – Jashwant May 04 '14 at 09:10
  • Thanks, but I am not trying to minify my JS or CSS files, they are already minified. I am trying to minify the html and that minification is interfering with the functionality of a javascript. – Tom May 04 '14 at 09:14
  • Because `$contents` contains not only `html` but also minified `css` and `js`. Have a look at this great project [minify](https://code.google.com/p/minify/). [HTML.php](https://code.google.com/p/minify/source/browse/min/lib/Minify/HTML.php) is a class specifically for html minification. – Jashwant May 04 '14 at 12:47
  • I did some testing using 3rd party html minification and the same issue occurs with those, so it is not the method used to minify, but the removal on newline characters. – Tom May 05 '14 at 02:36

1 Answers1

0

Got it solved. It was a result of the minified file essentially being one line and the double slashes // of a javascript comment were turning all JS after it into a continuation of that comment, hence non-functional.

I removed the javascript comment from the template and everything works beautifully.

I had planned to add JS comments to my regex, but as there is only one JS comment within all the templates it was easier to delete to comment, than add the extra processing to the code.

Tom
  • 2,928
  • 4
  • 28
  • 36