博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hexo 博客支持PWA和压缩博文
阅读量:7118 次
发布时间:2019-06-28

本文共 2057 字,大约阅读时间需要 6 分钟。

目标网站

PWA

yarn add hexo-offline

然后在root config.yml里新增

# offline config passed to sw-precache.service_worker:  maximumFileSizeToCacheInBytes: 5242880  staticFileGlobs:  - /**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,woff2}  - /lib/**/*.js  - /lib/**/*.css  - /images/*  - /js/src/**/*.js  stripPrefix: public  verbose: true  runtimeCaching:    - urlPattern: /*      handler: cacheFirst      options:        origin: cdn.bootcss.com

然后添加manifest.json, 比如我使用了 hexo-theme-next的主题,在layout/_custom/header.swig 中引用了manifest.json

manifest生成地址:

比如,我的为

{    "name": "风 - Ryan Miao",    "short_name": "风",    "theme_color": "#2196f3",    "background_color": "#2196f3",    "display": "browser",    "scope": "/",    "start_url": "/"  }

具体缓存策略还是看下官方文档,这里不求甚解缓存。重启博客,打开控制台,查看网络,会发现,所有的文件都是(from ServiceWorker) 或者(from disk cache)或者(from memory cache)

当hexo g之后,会多出一个service-worker.js里面则是会缓存的内容。

压缩

看了下计算,压缩大概可以节省一半空间。

$ npm install gulp -g$ npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save或者使用yarn yarn global add gulpyarn add gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp

然后,在根目录新增 gulpfile.js :

var gulp = require('gulp');var minifycss = require('gulp-minify-css');var uglify = require('gulp-uglify');var htmlmin = require('gulp-htmlmin');var htmlclean = require('gulp-htmlclean');// 压缩 public 目录 cssgulp.task('minify-css', function() {    return gulp.src('./public/**/*.css')        .pipe(minifycss())        .pipe(gulp.dest('./public'));});// 压缩 public 目录 htmlgulp.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'))});// 压缩 public/js 目录 jsgulp.task('minify-js', function() {    return gulp.src('./public/**/*.js')        .pipe(uglify())        .pipe(gulp.dest('./public'));});// 执行 gulp 命令时执行的任务gulp.task('default', [    'minify-html','minify-css','minify-js']);

运行:

hexo clean && hexo g && gulp &&  hexo s

参考

转载地址:http://cofel.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
Linux-PAM认证模块
查看>>
python 定时器
查看>>
linux磁盘阵列实战
查看>>
还是Jfinal上传遇到问题的小解决
查看>>
我的友情链接
查看>>
页面状态还是组件?到底什么才是交互的中心?
查看>>
我的友情链接
查看>>
根据JSP中日期控件传过来的日期来查询Oracle数据库中的数据
查看>>
VMware虚拟机安装DOS6.22(一)
查看>>
Lucene文档
查看>>
java移位运算
查看>>
我的友情链接
查看>>
Chapter 5 OpenStack镜像服务(Image service)
查看>>
关于搭建harbor企业级访问docker仓库
查看>>
PBD加密专家V6.5.168实例
查看>>
mysql学习笔记1
查看>>
vim
查看>>
Web服务
查看>>
androidkit发布0.5.3alpha版
查看>>