Append unique string or md5 hash to paths in html files to force refresh - remove users cache app.js -> app_8j3d7a4f.js
Supported script and link tags with src
or href
attributes.
Disclaimer This plugin is not 'gulp friendly' - it operates on files from outside of stream (Can't be fixed in this approach).
If you care about that try gulp-rev or it's relatives.
$ npm install --save-dev gulp-uncache
Before:
<!-- uncache -->
<script src="app.js"></script>
<!-- enduncache -->
<!-- uncache(rename:true, append:hash, srcDir:src, distDir:dist) -->
<link rel="stylesheet" href="style.css"/>
<!-- enduncache -->
After:
<script src="app.js?1401482624657"></script>
<link rel="stylesheet" href="style_46fa2c8d60.css"/>
Type String
default: time
String to append or one from:
time
- append actual time stamp
hash
- append md5 hash of file (prevent unnecessary refreshing file) need correct srcDir & distDir
Type Boolean
default: false
If set to true rename file otherwise append string as url query string
Type String
default: ./
Path to dir with source files. (Used only when
rename:true
, orappend:'hash'
)
Type String
default: ./
Path to dir where renamed files will be saved. (Used only when
rename:true
)
Type String
default {{path}}{{name}}_{{append}}.{{extension}}
Template for replace (Hogan.js). Available variables:
path
,name
,append
,extension
Type Function
Parameters: fileName
Function for transforming src file path At default returns
path.join(config.srcDir, fileName);
You can set options inline that way: (omit quotes sign)
<!-- uncache(param:value, param:value) -->
var gulp = require('gulp');
var uncache = require('gulp-uncache');
gulp.task('default', function () {
return gulp.src('src/index.html')
.pipe(uncache({
append: 'hash',
rename: true,
srcDir: 'src',
distDir: 'dist'
}))
.pipe(gulp.dest('dist'));
});
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<!--uncache-->
<link rel="stylesheet" href="style.css"/>
<!--enduncache-->
</head>
<body>
<!--uncache(rename:false)-->
<script src="js/file.js"></script>
<!--enduncache-->
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style_46fa2c8d60.css"/>
</head>
<body>
<script src="js/file.js?46fa2c8d60"></script>
</body>
</html>
var gulp = require('gulp');
var usemin = require('gulp-usemin');
var uncache = require('gulp-uncache');
gulp.task('default', function () {
return gulp.src('src/index.html')
.pipe(usemin({
js: []
}))
.pipe(uncache())
.pipe(gulp.dest('dist'));
});
<!--uncache-->
<!-- build:js lib.js -->
<script src="js/file1.js"></script>
<script src="js/file2.js"></script>
<script src="js/file3.js"></script>
<!-- endbuild -->
<!--enduncache-->
<script src="lib.js?1401393153336"></script>
- 'uncache' for all sources in files without
<!--uncache-->
tags - support css images (e.g. for often changing css sprites image)
- option template
- inline options
- fixed parsing regexp
- option rename
- option append hash
- allow both
'
and"
in tags
- added append option
- fixed multi line blocks
- fixed parsing lines with attributes
- skipping incorrect tags
- log info and errors
- initial release
MIT © Maciej Dudziński