[Suggestion] different headers for some files
sp90 opened this issue · comments
Hi thanks for a great plugin,
I want to know how to set different headers for different files, is this possible ?
This could work nicely if it could be extended like this:
var defaultHeaders = {};
.pipe(publisher.publish(function(filepath) {
// even better if it supported globs
if (filepath === './dist/index.html') {
return {
'Cache-Control': 'max-age=315360000, no-transform, public'
}
}
return defaultHeaders;
}))
I made this work by doing 2, with different cache file names, and then setting the index file that i want to add a different header. And in the second task only do file upload
gulp.task('copyIndexToS3', function() {
// If the aws file are unavailable do nothing
if (!awsKeys) {
// To setup permissions, and policies check the article linked in the top
console.log('Setup your aws keys, and bucket correctly guidelines are found in the link in gulp/cdn-s3.js file');
return gulp.src('./dist/**');
}
var keysObj = live === true ? awsKeys.prod : awsKeys.dev;
var publisher = awspublish.create(keysObj, {
cacheFileName: '.awspublish-' + env + '.example.com-index'
});
return gulp.src([
'dist/index.html'
])
.pipe(awspublish.gzip())
.pipe(publisher.publish({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Expires': 0
}, {
createOnly: true
}))
.pipe(publisher.cache())
.pipe(awspublish.reporter());
});
gulp.task('copyToS3', function() {
// If the aws file are unavailable do nothing
if (!awsKeys) {
// To setup permissions, and policies check the article linked in the top
console.log('Setup your aws keys, and bucket correctly guidelines are found in the link in gulp/cdn-s3.js file');
return gulp.src('./dist/**');
}
var keysObj = live === true ? awsKeys.prod : awsKeys.dev;
var publisher = awspublish.create(keysObj, {
cacheFileName: '.awspublish-' + env + '.example.com'
});
return gulp.src([
'dist/**',
'!dist/index.html',
'!dist/**/*.test*',
'!dist/rev-manifest.json',
])
.pipe(awspublish.gzip())
.pipe(publisher.publish({
'Cache-Control': 'max-age=315360000, no-transform, public'
}))
.pipe(publisher.cache())
.pipe(publisher.sync())
.pipe(awspublish.reporter());
});
@sp90 could you post an example... Currently I would want to set max-age=0
for index.html
page but for js
, css
files I would like to set max-age=315360000
. Thanks
@martinmicunda see post above
But i do think that it should have a feature to customize cache for each file maybe a function run for each file that can be injected in publish
ex
.pipe(publisher.publish(function(filepath) {
if (filepath === './dist/index.html') {
return {
'Cache-Control': 'max-age=315360000, no-transform, public'
}
}
}))
@sp90 thanks! Yeah I agree it should be implemented in the plugin. I am facing an issue when I set header max-age=315360000
and I invalidate cloudfront cache then the user sometimes need to refresh the page to get latest copy of index.html
.
Related to #23?