hexo优化系列

图片优化

本站采用sm.ms图床,虽然速度不及某博,但是算是稳定方便,速度也还行。

百度谷歌优化

百度站长平台会进行扫描分析,按照操作即可。

gulp进行html/css/js混淆压缩

安装gulp以及插件

npm i -g gulp
npm i --save gulp-clean-css gulp-uglify gulp-htmlmin gulp-htmlclean

验证版本以及依赖信息(出现以下即代表成功)

C:\Users\28731>gulp --version
CLI version: 2.2.0
Local version: 4.0.2

在package.json中可看到相关依赖:

"gulp": "^4.0.2",
"gulp-clean-css": "^4.2.0",
"gulp-htmlclean": "^2.7.22",
"gulp-htmlmin": "^5.0.1",
"gulp-uglify": "^3.0.2"

创建gulpfile.js配置文件

var gulp = require('gulp');
var cleancss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');

/*压缩CSS*/
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(cleancss())
.pipe(gulp.dest('./public'));
});

/*压缩html文件*/
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}))
.pipe(gulp.dest('./public'));
});

/*压缩JS文件*/
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});

gulp.task('build', gulp.series('minify-html', 'minify-css', 'minify-js'));

实验打包发布验证

hexo clean
hexo g
gulp build
hexo s (可选)
hexo d

图片进行 lazyload 懒加载

https://appelsiini.net/projects/lazyload/
https://www.cnblogs.com/yzg1/p/5051554.html

设置防抖和节流

function debounce(func, wait, immediate) {
var timeout
return function () {
var context = this
var args = arguments
var later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
};

function throttle(func, wait, mustRun) {
var timeout
var startTime = new Date()

return function () {
var context = this
var args = arguments
var curTime = new Date()

clearTimeout(timeout)
if (curTime - startTime >= mustRun) {
func.apply(context, args)
startTime = curTime
} else {
timeout = setTimeout(func, wait)
}
}
};

CSS样式表放置于头部

JS文件尽量外链CDN,内部JS放置于尾部使得网页先展示所有节点

部分功能接口可采取延时加载

Author: 𝓣𝓪𝓭𝓶
Link: https://liuhongwei3.github.io/2019/11/24/hexo%E4%BC%98%E5%8C%96%E7%B3%BB%E5%88%97/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.