简体   繁体   English

缩小HTML代码而不破坏内联Javascript

[英]Minify HTML code without breaking inline Javascript

I have been caching rendered pages to a redis cache and serving them to non logged in users as a speedy cache. 我一直在将呈现的页面缓存到Redis缓存中,并将它们作为快速缓存提供给未登录的用户。 The page size for most pages is hitting around 100kb. 大多数页面的页面大小约为100kb。 I was able to knock 20kb off the size by minifying the html before inserting it into redis, but it seems that this process breaks any inline javascript on the pages. 通过将html缩小到redis之前,我能够缩小20kb的大小,但是似乎此过程破坏了页面上的所有内联JavaScript。

Am using the following PHP function to perform the minify at the moment. 我正在使用以下PHP函数来执行缩小。 I have had to disable it of course, but it seems to be doing OK, just needs to be more weary of javascript. 当然,我不得不禁用它,但它似乎做得不错,只需要对JavaScript更加满意即可。

function MinifyHtml($html)
{
    $search = array(
        '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
        '/[^\S ]+\</s',     // strip whitespaces before tags, except space
        '/(\s)+/s',         // shorten multiple whitespace sequences
        '/<!--(.|\s)*?-->/' // Remove HTML comments
    );

    $replace = array('>','<','\\1','');

    $buffer = preg_replace($search, $replace, $html);

    return $buffer;
}

我已经使用PHP函数缩小HTML和JavaScript的范围,可以在这里找到: https : //gist.github.com/tovic/d7b310dea3b33e4732c0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM