Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] different headers for some files #136

Open
sp90 opened this issue Dec 15, 2016 · 5 comments
Open

[Suggestion] different headers for some files #136

sp90 opened this issue Dec 15, 2016 · 5 comments

Comments

@sp90
Copy link

sp90 commented Dec 15, 2016

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;
}))
@sp90 sp90 changed the title [Help] [Help] different headers for some files Dec 15, 2016
@sp90
Copy link
Author

sp90 commented Dec 15, 2016

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());
});

@martinmicunda
Copy link

@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

@sp90
Copy link
Author

sp90 commented Dec 15, 2016

@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'
		}
	}
}))

@martinmicunda
Copy link

@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.

@sp90 sp90 changed the title [Help] different headers for some files [Suggestion] different headers for some files Dec 15, 2016
@skeggse
Copy link

skeggse commented Feb 20, 2019

Related to #23?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants