优化网页访问速度,压缩网站页面多余的空格和换行。

文章分类:Web前端

在页面生成的后处理中(如:django的process_response middleware, yii的processOutput,其它的同理),加入下面这么一段代码,假设output是将要输出的页面HTML代码。

 

 

PHP:

$output = preg_replace("/([\t ]+)/", " ", $output);
$output = preg_replace("/(\s*\n\s*)/", "", $output);

 

Python:

output= re.sub(r"([\t ]+)", " ", output)<br />output= re.sub(r"(\s*\n\s*)", "", output)

 

 

有了这么两行代码后,你会发现新的页面HTML源码就只有一行了,多余的空格和换行全部压缩掉了,到汽车4S网 看看效果吧(用浏览器查看网站页面源代码),压缩了页面的字节,一定程度上可以提升网站的加载时间,从而提高网站的访问速度哦,赶紧给你的网站也加上吧!

ref 

http://felinx.javaeye.com/blog/682992



优化网页访问速度,压缩网站页面多余的空格和换行。

相关文章:

  • 2021-09-27
  • 2021-11-18
  • 2021-11-06
  • 2022-01-08
  • 2021-05-25
  • 2022-12-23
  • 2021-10-21
猜你喜欢
  • 2021-06-03
  • 2021-09-15
  • 2022-12-23
  • 2022-01-11
  • 2021-08-21
  • 2021-12-07
  • 2021-12-25
相关资源
相似解决方案