@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
171 lines (151 loc) • 4.19 kB
JavaScript
const gulp = require( 'gulp' );
const awspublish = require( 'gulp-awspublish' );
const awspublishRouter = require( 'gulp-awspublish-router' );
/**
* @description Builds smart caching routes for gulp-awspublish-router.
*
* Strategy:
* - HTML files: No cache (always fetch fresh to get latest asset references)
* - Hashed assets (JS/CSS with content hashes): Long cache + immutable
* - JSON metadata files: Short cache (may change between builds)
* - Other static assets (fonts, images): Moderate cache
*
* @since 8.2.1
*
* @return {Object} Routes configuration for awspublish-router.
*/
function getSmartCacheRoutes() {
// Note: Each route MUST have a 'key' property to specify the S3 key.
// '$&' means use the matched filename as-is.
return {
// HTML files - never cache, always fetch fresh
'^.+\\.html$': {
key: '$&',
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
},
},
// JSON metadata files - revalidate on every request
'^.+\\.json$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=0, must-revalidate',
},
},
// Hashed JS bundles (e.g., main.abc123.iframe.bundle.js)
'^.+\\.[a-f0-9]{6,32}\\.(iframe\\.bundle|manager\\.bundle|bundle)\\.js$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=31536000, immutable, public',
},
},
// Hashed JS files (e.g., file.abc123.js)
'^.+\\.[a-f0-9]{6,32}\\.js$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=31536000, immutable, public',
},
},
// Hashed CSS files
'^.+\\.[a-f0-9]{6,32}\\.css$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=31536000, immutable, public',
},
},
// Hashed source maps
'^.+\\.[a-f0-9]{6,32}\\..+\\.map$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=31536000, immutable, public',
},
},
// Non-hashed source maps - 1 day cache
'^.+\\.map$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=86400, public',
},
},
// Font files - 1 week cache
'^.+\\.(woff2?|ttf|eot|otf)$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=604800, public',
},
},
// Image files - 1 week cache
'^.+\\.(ico|svg|png|jpe?g|gif|webp)$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=604800, public',
},
},
// Non-hashed CSS - 1 hour cache
'^.+\\.css$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=3600, public',
},
},
// Non-hashed JS - 1 hour cache
'^.+\\.js$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=3600, public',
},
},
// Default fallback - 1 hour cache
'^.+$': {
key: '$&',
headers: {
'Cache-Control': 'max-age=3600, public',
},
},
};
}
module.exports = {
sync( path, params ) {
const config = params?.config;
const headers = params?.headers || {};
const options = params?.options || {};
const useSmartCaching = params?.useSmartCaching !== false;
if ( ! path || ! config ) {
console.log( 'You must supply an aws config and output path to run publish.' );
return;
}
if ( ! config.credentials.accessKeyId || ! config.credentials.secretAccessKey ) {
console.log( 'You must supply an aws access key id and secret access key to run publish.' );
return;
}
const publisher = awspublish.create( config );
if ( useSmartCaching ) {
console.log( 'Using smart per-file caching strategy:' );
console.log( ' - HTML files: no-cache (always fresh)' );
console.log( ' - Hashed assets: 1 year + immutable' );
console.log( ' - Static assets: 1 week' );
console.log( ' - Other files: 1 hour' );
// Use awspublish-router for route-based cache headers
return (
gulp
.src( path )
.pipe( awspublishRouter( {
routes: getSmartCacheRoutes(),
} ) )
.pipe( publisher.publish( {}, options ) )
.pipe( publisher.sync() )
.pipe( publisher.cache() )
.pipe( awspublish.reporter() )
);
}
// Legacy mode - use single cache header for all files
return (
gulp
.src( path )
.pipe( publisher.publish( headers, options ) )
.pipe( publisher.sync() )
.pipe( publisher.cache() )
.pipe( awspublish.reporter() )
);
},
};