前端静态资源自动化处理版本号防缓存

前端静态资源自动化处理版本号防缓存

浏览器会默认缓存网站的静态资源文件,如:js文件、css文件、图片等。缓存带来网站性能提高的同时也带来了一些困扰,最多见的问题就是不能及时更新静态资源,形成新版本发布时用户没法及时看到新版本的变化,严重影响了用户体验。javascript

上述问题,最简单的办法就是在资源的请求路径上添加版本号,格式以下:css

url?v=1.0.0

每次在更改资源的时候,手动修改版本号,可是每次手动改那么多后缀有些费事,如今有不少的工具可让咱们更轻松的完成这项工具。本文将探讨使用目前最流行的前端构建工具 Gulp 和 Webpack 自动化为静态资源添加版本号防缓存处理。html

使用 Gulp 处理文件版本

Gulp 是一个简单易用的前端自动化构建工具,很是适合于构建多页面的工做流程。前端

安装 Gulp(这里使用的是 Gulp 4+ 版本):java

$ npm install --save-dev gulp

安装 gulp-rev 插件:webpack

$ npm install --save-dev gulp-rev

gulp-rev 插件的做用就是为静态资源添加版本号。git

新建 gulpfile.js 文件:github

const gulp = require('gulp');
const rev = require('gulp-rev');

// 添加版本号
gulp.task('rev', () => {
  return gulp.src('src/css/*.css')
    .pipe(rev()) // 将全部匹配到的文件名所有生成相应的版本号
    .pipe(gulp.dest('dist/css'))
    .pipe(rev.manifest()) //把全部生成的带版本号的文件名保存到rev-manifest.json文件中
    .pipe(gulp.dest('rev/css')) //把rev-manifest.json文件保存到指定的路径
});

执行 rev 任务后,rev/css 文件加下多了一个 rev-manifest.json 文件。web

rev-manifest.json 文件的内容以下:npm

{
  "index.css": "index-35c63c1fbe.css"
}

而后,安装 gulp-rev-collector 插件:

$ npm install --save-dev gulp-rev-collector

gulp-rev-collector 插件主要是配合 gulp-rev 替换文件版本号。

修改 gulpfile.js 文件:

const gulp = require('gulp');
const rev = require('gulp-rev');

// 添加版本号
gulp.task('rev', () => {
  return gulp.src('src/css/*.css')
    .pipe(rev()) // 将全部匹配到的文件名所有生成相应的版本号
    .pipe(gulp.dest('dist/css'))
    .pipe(rev.manifest()) //把全部生成的带版本号的文件名保存到rev-manifest.json文件中
    .pipe(gulp.dest('rev/css')) //把rev-manifest.json文件保存到指定的路径
});

const revCollector = require('gulp-rev-collector');

// 控制文件版本号
gulp.task('rev-collector', () => {
  return gulp.src(['rev/**/*.json', 'src/**/*.html'])
    .pipe(revCollector({
      replaceReved: true
    }))
    .pipe(gulp.dest('dist'))
})

gulp.task('default', gulp.series('clean', 'rev', 'rev-collector'))

执行 gulp 默认任务。检查 dist 下 index.html 文件 css 的版本是否替换成功。

使用 Webpack 处理文件版本

Webpack 是一个现代 JavaScript 应用程序的静态模块打包器,很是适合于构建单页面的工做流程,固然也能够构建多页面的工做流程。

安装 Webpack(这里使用的是 webpack 4+ 版本)。

$ npm install --save-dev webpack webpack-cli

经过使用 output.filename 进行文件名替换,webpack 使用 [chunkhash] 替换文件名,在文件名中包含一个 chunk 相关(chunk-specific)的哈希。

安装 clean-webpack-plugin 插件(清理文件夹):

$ npm install --save-dev clean-webpack-plugin

clean-webpack-plugin 插件的做用是清理文件夹,因为每次打包的文件版本不一样,输出目录会生成不少不一样版本的目标文件,因此须要清理文件夹。

配置文件 webpack.config.js 以下:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.[chunkhash:5].js', //这里设置 [chunkhash] 替换文件名,数字5为 chunkhash 的字符长度。
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new CleanWebpackPlugin(['dist'])
  ]
}

在 src 目录新建一个 index.html 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Webpack实现静态资源版本管理自动化</title>
  </head>
  <body>
    <script src="index.js"></script>
  </body>
</html>

安装 html-webpack-plugin 插件:

$ npm install --save-dev html-webpack-plugin

html-webpack-plugin 插件编译 html 替换带有哈希值版本信息的资源文件。

修改 webpack.config.js 文件:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.[chunkhash:5].js', //这里设置 [chunkhash] 替换文件名,数字5为 chunkhash 的字符长度。
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'Webpack实现静态资源版本管理自动化'
    })
  ]
}

html-webpack-plugin 默认入口文件为 index.html,具体的参数配置请参考https://www.npmjs.com/package/html-webpack-plugin
关于 Webpack 处理缓存的更多教程请移步官方文档。

符录

usuallyjs函数库: https://github.com/JofunLiang/usuallyjs

相关文章
相关标签/搜索