Skip to content

Instantly share code, notes, and snippets.

@skellertor
Last active October 18, 2015 08:45
Show Gist options
  • Save skellertor/c00f1706c42747dcdc01 to your computer and use it in GitHub Desktop.
Save skellertor/c00f1706c42747dcdc01 to your computer and use it in GitHub Desktop.
Custom gulp tasks

Gulp task to replace the src attribute of an html script element so the browser doesn't cache it.

var replace = require('gulp-replace');

function cache_version() {
    return (Math.floor((Math.random() * 1000) + 1)).toString(); 
}

gulp.task('version', function () {
    var version = cache_version();
    var regex = /src(\s)?=(\s)?["'][\S]*(\.)js/g;
    gulp.src('src/*.html')
    .pipe(replace(regex, function (found) {
        return found += '?v=' + version;
    }))
    .pipe(gulp.dest('build'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment