Answer the question
In order to leave comments, you need to log in
How to write date at the end of CSS file in GULP?
Greetings, the question may not have been quite correctly posed, but the point is, how to add a date to the final css file in GULP? This is necessary so that the static version of the file is updated, and so that the client does not clear the browser cache for updating.
Did you usually do this with your hands before without GULP? added ?v=3 at the end of CSS and so on 4,5,6
<link rel="stylesheet" type="text/css" href="/css/style.css?v=3">
<link rel="stylesheet" type="text/css" href="/css/style.css?v=202102081545">
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) { var month = '0'+month; }
if(day.toString().length == 1) { var day = '0'+day; }
if(hour.toString().length == 1) { var hour = '0'+hour; }
if(minute.toString().length == 1) { var minute = '0'+minute; }
if(second.toString().length == 1) { var second = '0'+second; }
var dateTime = year+'-'+month+'-'+day+'T'+hour+':'+minute+':'+second;
return dateTime;
}
// styles
const styles = () => {
return src(config.styles.src)
.pipe(
sass({
outputStyle: "expanded"
})
)
.pipe(
group_media()
)
.pipe(postcss([
require('autoprefixer'),
require('postcss-discard-comments'),
require('postcss-csso')
]))
.pipe(
rename({
extname: ".min.css"
//extname: ".min.css?v=" + getDateTime() //если так сделать ошибка, не получается
})
)
.pipe(dest(config.styles.dist))
.pipe(server.stream());
}
Answer the question
In order to leave comments, you need to log in
<link
rel="stylesheet"
href="/css/style.css?v=%NO_CACHE%"
>
const replace = require('gulp-replace');
gulp.task('html', function(){
gulp.src(['index.html'])
// … other pipes
.pipe(replace('%NO_CACHE%', (new Date)->getTime()))
.pipe(gulp.dest('build/'));
});
const replace = require('gulp-replace');
gulp.task('html', function(){
gulp.src(['index.html'])
.pipe(replace('%NO_CACHE%', function() {
const md5hash = checkCss('style.css'); // read css file and calculate md5 hash
return md5hash;
}))
.pipe(gulp.dest('build/'));
});
The most popular Gulp file versioning plugin is gulp-rev . There is also the gulp-rev-all plugin , which takes into account changes not only to the file itself, but also to its dependencies (for example, if an image or font connected in css changes, this plugin will see this and also update the version of the style file).
You need something like gulp-buster . It will create a file with the hash of the files and use that in php as a version.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question