To prevent the client from caching static resources, we need to rename the static resources by MD5 or timestamps every time we update the CSS or JS. Allows the client to re-request the resource instead of fetching it from the cache. The SRC in the HTML template is then modified accordingly. Of course, there is an additional requirement that static resources need to be optimized for themselves (compression merges).
Here is the code for my gulpfile.js:
varGulp = require ('Gulp'),//Base LibraryClean = require ('Gulp-clean'),//Clear FolderMinify = require ('Gulp-minify-css'),//CSS CompressionRename = require ('Gulp-rename'),//file RenameRev = require ('Gulp-rev'),//Change the version nameCollector = require ('Gulp-rev-collector'),//Gulp-rev plug-in for HTML file change reference pathConcat = require ('Gulp-concat'),//Merging multiple FilesNotify = require ('gulp-notify');//Tips Gulp.task (' Clean', function () {returnGULP.SRC ('Build', {read:false})//The second parameter of the src {read:false}, is to not read the file, speeding up the program. . Pipe (Clean ());}) Gulp.task ('Index',[' Clean'],function () {returnGULP.SRC ('app/index.min.html'). Pipe (Rename (path) {Path.basename='Index'; Path.extname=". html"; }). Pipe (Gulp.dest ('Build')) }) Gulp.task ('CSS',['Index'],function (CB) {returnGULP.SRC ('App/**/*.css'). Pipe (Minify ()). Pipe (Concat ('Main.css') . Pipe (Rename (path) {Path.basename+='. Min'; Path.extname=". CSS"; }). Pipe (rev ()). Pipe (Gulp.dest ('Build/css') . Pipe (Rev.manifest ()). Pipe (Gulp.dest ('Build/rev')); CB ();}) Gulp.task ('Rev',['CSS'],function () {returnGULP.SRC (['Build/rev/rev-manifest.json','build/index.html']). Pipe (Collector ({replacereved:true}). Pipe (Gulp.dest ('build/'). Pipe (Notify ("Success!!!"))})
My directory structure:
The app is the original code directory, build is gulp generated
Gulp Css,js Compression Merge encryption replacement