Typecho博客优化 静态HTML首页生成 网站打开速度提高数倍


介绍

很多typecho博客打开速度比较慢,测试来看均值基本都在3秒以上,因为博客上有不少的CSSJS脚本,导致加载速度偏慢,并且很多博客没有做图站分离,都是放在一起的,速度较慢;

于是给首页做了个静态HTML,首页打开速度提升了数倍,需要的朋友也可以试试;

未做静态HTML前的打开测试

未生成.png

生成静态HTML前的打开测试

生成.jpg

对比可以看出提升确实比较大,从均值3.5秒提升到了1秒以内,相当满意了;

开始

这里提供两个PHP代码脚本,一个是不需要密码的,一个是需要密码的;
首先在网站根目录新建一个php文件,文件命名为:jingtai.php
然后在其中写入需要的代码(不需要密码和需要密码的选择其中一个即可)

不需要密码的PHP脚本
<pre><div class="enlighter-default enlighter-v-standard enlighter-t-dracula enlighter-hover enlighter-overflow-scroll"><div class="enlighter"><div class=""><div><span class="enlighter-g1">&lt;</span><span class="enlighter-text">?php $nowtime=</span><span class="enlighter-m0">time</span><span class="enlighter-g1">()</span><span class="enlighter-text">; $pastsec = $nowtime - $_GET</span><span class="enlighter-g1">[</span><span class="enlighter-s0">&#34;t&#34;</span><span class="enlighter-g1">]</span><span class="enlighter-text">; </span><span class="enlighter-k1">if</span><span class="enlighter-g1">(</span><span class="enlighter-text">$pastsec</span><span class="enlighter-g1">&lt;</span><span class="enlighter-n1">600</span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span><span class="enlighter-g1">{</span><span class="enlighter-text"> exit;</span><span class="enlighter-c0"> //10分钟更新一次,时间可以自己调整 } ob_start(); //打开缓冲区 include(&#34;index.php&#34;); $content = ob_get_contents(); //得到缓冲区的内容 $content .= &#34;n&lt;script language=javascript src=&#34;jingtai.php?t=&#34;.$nowtime.&#34;&#34;&gt;&lt;/script&gt;&#34;; //加上调用更新程序的代码 file_put_contents(&#34;index.html&#34;,$content); if (!function_exists(&#34;file_put_contents&#34;)) { function file_put_contents($fn,$fs) { $fp=fopen($fn,&#34;w+&#34;); fputs($fp,$fs); fclose($fp); } } ?&gt;</span></div></div></div></div></pre>
需要要密码的PHP脚本
<pre><div class="enlighter-default enlighter-v-standard enlighter-t-dracula enlighter-hover enlighter-overflow-scroll"><div class="enlighter"><div class=""><div><span class="enlighter-g1">&lt;</span><span class="enlighter-text">?php </span><span class="enlighter-c1">/** * 首页静态化脚本 */</span><span class="enlighter-text"> </span><span class="enlighter-m0">ini_set</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;date.timezone&#39;</span><span class="enlighter-text">, </span><span class="enlighter-s0">&#39;PRC&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text">; </span><span class="enlighter-c1">/* 缓存过期时间 单位:秒 */</span><span class="enlighter-text"> $expire = </span><span class="enlighter-n1">600</span><span class="enlighter-text">; </span><span class="enlighter-c1">/* 主动刷新密码 格式:https://你的域名/jingtai.php?password=123456 */</span><span class="enlighter-text"> $password = </span><span class="enlighter-s0">&#39;123456&#39;</span><span class="enlighter-text">; $file_time = @</span><span class="enlighter-m0">filemtime</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;index.html&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text">; </span><span class="enlighter-m0">time</span><span class="enlighter-g1">()</span><span class="enlighter-text"> - $file_time </span><span class="enlighter-g1">&gt;</span><span class="enlighter-text"> $expire </span><span class="enlighter-g0">&amp;&amp;</span><span class="enlighter-text"> </span><span class="enlighter-m0">create_index</span><span class="enlighter-g1">()</span><span class="enlighter-text">; </span><span class="enlighter-m0">isset</span><span class="enlighter-g1">(</span><span class="enlighter-text"> $_GET</span><span class="enlighter-g1">[</span><span class="enlighter-s0">&#39;password&#39;</span><span class="enlighter-g1">]</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span><span class="enlighter-g0">&amp;&amp;</span><span class="enlighter-text"> $_GET</span><span class="enlighter-g1">[</span><span class="enlighter-s0">&#39;password&#39;</span><span class="enlighter-g1">]</span><span class="enlighter-text"> == $password </span><span class="enlighter-g0">&amp;&amp;</span><span class="enlighter-text"> </span><span class="enlighter-m0">create_index</span><span class="enlighter-g1">()</span><span class="enlighter-text">; </span><span class="enlighter-c1">/** * 生成 index.html */</span><span class="enlighter-text"> </span><span class="enlighter-k1">function</span><span class="enlighter-text"> </span><span class="enlighter-m0">create_index</span><span class="enlighter-g1">()</span><span class="enlighter-text"> </span><span class="enlighter-g1">{</span><span class="enlighter-text"> </span><span class="enlighter-m0">ob_start</span><span class="enlighter-g1">()</span><span class="enlighter-text">; </span><span class="enlighter-m0">include</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;index.php&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text">; $content = </span><span class="enlighter-m0">ob_get_contents</span><span class="enlighter-g1">()</span><span class="enlighter-text">; $content .= </span><span class="enlighter-s0">&#34;n&lt;!-- Create time: &#34;</span><span class="enlighter-text"> . </span><span class="enlighter-m0">date</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;Y-m-d H:i:s&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text"> . </span><span class="enlighter-s0">&#34; --&gt;&#34;</span><span class="enlighter-text">; </span><span class="enlighter-c1">/* 调用更新 */</span><span class="enlighter-text"> $content .= </span><span class="enlighter-s0">&#34;n&lt;script language=javascript src=&#39;jingtai.php&#39;&gt;&lt;/script&gt;&#34;</span><span class="enlighter-text">; </span><span class="enlighter-m0">ob_clean</span><span class="enlighter-g1">()</span><span class="enlighter-text">; $res = </span><span class="enlighter-m0">file_put_contents</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;index.html&#39;</span><span class="enlighter-text">, $content </span><span class="enlighter-g1">)</span><span class="enlighter-text">; </span><span class="enlighter-k1">if</span><span class="enlighter-text"> </span><span class="enlighter-g1">(</span><span class="enlighter-text"> $res !== </span><span class="enlighter-k1">false</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text"> </span><span class="enlighter-g1">{</span><span class="enlighter-text"> </span><span class="enlighter-m0">die</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;Create successful&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text">; </span><span class="enlighter-g1">}</span><span class="enlighter-text"> </span><span class="enlighter-k1">else</span><span class="enlighter-text"> </span><span class="enlighter-g1">{</span><span class="enlighter-text"> </span><span class="enlighter-m0">die</span><span class="enlighter-g1">(</span><span class="enlighter-text"> </span><span class="enlighter-s0">&#39;Create error&#39;</span><span class="enlighter-text"> </span><span class="enlighter-g1">)</span><span class="enlighter-text">; </span><span class="enlighter-g1">}</span><span class="enlighter-text"> </span><span class="enlighter-g1">}</span></div></div></div></div></pre>
保存为utf8编码后退出;
注意:如果使用的宝塔面板,还要在后台网站设置-默认文档-将index.html的排序上调至第一位
然后在浏览器中打开PHP脚本链接即可
打开链接后会在网站根目录下生成一个index.html的静态文件,首页静态化也就完成了;
不需要密码脚本的链接:https://你的域名/jingtai.php
需要密码脚本的链接:https://你的域名/jingtai.php?password=123456

说明

两个PHP脚本中的更新时间默认为600秒,也就是十分钟更新一次,默认密码为123456
更新时间及访问密码都可以自行设定,修改脚本中的数值即可;

验证

查看是否生效,浏览器中重新打开你的网站首页,右键查看源代码
不需要密码的脚本
在最后一行如果有<script language=javascript>...之类的字眼,说明你访问的就是index.html的页面
需要密码的脚本
在最后一行会有显示最后一次更新时间
<!-- Create time: 2020-03-09 00:33:41 -->
<script language=javascript src='jt_index.php'></script>

当然也可以测试工具进行网站打开测试;
网站测试:https://www.boce.com/tool

本文转自mvpsky站qqq著:原文链接

最后修改:2021 年 06 月 06 日
如果觉得我的文章对你有用,请随意赞赏