Skip to content

Instantly share code, notes, and snippets.

@dlutwuwei
Created May 20, 2014 10:30
Show Gist options
  • Save dlutwuwei/1bd524ccd52305724dcf to your computer and use it in GitHub Desktop.
Save dlutwuwei/1bd524ccd52305724dcf to your computer and use it in GitHub Desktop.
hexo速度优化

title: hexo速度优化(light主题) date: 2014-05-20 9:00:50 categories: hexo tags: [hexo, 优化, jquery,googleapis]

hexo是利用github.io实现的一个博客系统,它在本地生成静态页面上传到github,为广大码农提供了一个免费的博客托管,网上很多教程,只教大家配置各种插件,发现打开网页还要载入大半天,甚至报一大堆脚本错误,虽然不影响使用,但是让人看着很难受。

enter image description here

看了一下发现google的cdn被墙的太厉害,jquery官网主站都因为这个打不开。 jqurey下载时间高达43s。只能弃用了,试了一下发现直接放在github上速度还可以接受。 方法: 找到路径hexo\themes\light\layout\_partial下的after_footer.ejs,修改第一行代码 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>,改为从本地获取, <script src="<%- config.root %>js/jquery.min.js"></script> 然后下载一个jquery.min.js放入hexo\themes\light\source\js中。

需要注意的是,jquery.min.js里的注释里如果带有//@ sourceMappingURL=jquery.min.map需要去除,否则这个脚本又会去下载jquery.min.map,和jquery.js,继续被阻塞,因为你用的是可调试版本,jqueyr.min.map存储了压缩后变量名和源码里名称的对照表,启动map调试后(只有chrome支持,而且是默认开启),可以看到压缩前的变量名,很坑chrome用户。

同样google字体的下载页够慢的,明显是天朝故意的!

enter image description here

对此我们也只能复制一个放在本地了,\hexo\themes\light\source\css\_base\variable.styl里修改路径。@import url("//fonts.googleapis.com/css?family=Lato:400,400italic")修改为@import url("/css/font/9k-RPmcnxYEPm8CNFsH2gg.woff")

hexo s运行完后发现,还有一个woff文件从google下9k-RPmcnxYEPm8CNFsH2gg.woff,找了半天才发现,font-title = 'letao'.... 自动触发了下载,应该好好研究CSS3了,不明就里,新建一个leto字体(@font-fasce)加入其中,注意font-title的复制要在leto字体定义之后,leto字体引用了9k-RPmcnxYEPm8CNFsH2gg.woff,把他也复制进font文件夹,好了,运行,终于远离了googleapis

如果不好使,把主文件夹的public删除,hexo g经常不更新css,不知道他是怎么判断文件更新的。

#varible.styl
// Color
color-background = #FFFFFF
color-font = #444
color-meta = #999
color-border = #ddd
color-link = #258fb8

// Font
@import url("/css/font/fontawesome-webfont.woff")

font-icon = FontAwesome
font-icon-path = 'font/fontawesome-webfont'

@font-face {
  font-family 'FontAwesome'
  font-style normal
  font-weight normal
  src url(font-icon-path.eot?#iefix) format('embedded-opentype'), url(font-icon-path.woff) format('woff'), url(font-icon-path.ttf) format('truetype'), url(font-icon-path.svg#FontAwesomeRegular) format('svg')
}

@font-face {
  font-family: 'Lato';
  font-style: normal;
  font-weight: 400;
  src: local('Lato Regular'), local('Lato-Regular'), url(/css/font/9k-RPmcnxYEPm8CNFsH2gg.woff) format('woff');
}


font-default = "Helvetica Neue", "Helvetica", Arial, sans-serif
font-serif = "Georgia", serif
font-mono = Monaco, Menlo, Consolas, Courier New, monospace
font-title = Lato
// Width
page-width = 1200px

enter image description here

看到没,速度快了30倍。

对于其他脚本合并之类的优化,暂时不用,本身脚本文件不多,如果还想速度上个层次,可以使用国内的托管,如gitcafe。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment