diff --git a/CHANGELOG.md b/CHANGELOG.md index 412acb4..c262899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # barebones changelog +## 2.0.4 +* Better static asset revisioning using randomly generated hash when running gulp tasks. + ## 2.0.3 * update get_post_thumbnail_url function and add ability to get specific size. diff --git a/functions.php b/functions.php index ae1a9c5..f16dbcc 100644 --- a/functions.php +++ b/functions.php @@ -72,7 +72,7 @@ function barebones_scripts() wp_deregister_script('jquery'); wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3', true); wp_enqueue_script('jquery'); - wp_enqueue_script('script', get_stylesheet_directory_uri() . '/js/script.min.js?' . time(), ['jquery'], null, true); + wp_enqueue_script('script', get_stylesheet_directory_uri() . '/js/script.min.js?' . getPackageHash(), ['jquery'], null, true); } add_action('wp_enqueue_scripts', 'barebones_scripts'); @@ -173,7 +173,18 @@ function barebones_tiny_mce_before_init($settings) add_filter('tiny_mce_before_init', 'barebones_tiny_mce_before_init'); +/** + * Get hash from package.json used for assets url hash + * @return string + */ +function getPackageHash() +{ + $package = file_get_contents(get_bloginfo( 'stylesheet_directory' ) . "/package.json"); + $packageJson = json_decode($package, true); + // if there is problem, fallback to time. + return isset($packageJson['hash']) ? $packageJson['hash'] : time(); +} /** * Get post thumbnail url diff --git a/gulpfile.js b/gulpfile.js index ad8a94d..77073ff 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,7 +2,6 @@ // process.env.DISABLE_NOTIFIER = true; var gulp = require('gulp'); -var gulpImagemin = require('gulp-imagemin'); var elixir = require('laravel-elixir'); // Assets path @@ -15,15 +14,34 @@ elixir.config.css.autoprefix.options.browsers = ['last 15 versions']; var Task = elixir.Task; elixir.extend('imagemin', function(src, dest) { new Task('imagemin', function() { + var gulpImagemin = require('gulp-imagemin'); return gulp.src(elixir.config.assetsPath + src) .pipe(gulpImagemin()) .pipe(gulp.dest(dest)); }).watch(elixir.config.assetsPath + src); }); +// Create a Hash in package.json +elixir.extend('hash', function() { + new Task('hash', function() { + var fs = require('fs'); + var fileName = './package.json'; + var file = require(fileName); + + // generate a new hash + file.hash = ( 0 | Math.random() * 9e6 ).toString(36); + // save to package.json + fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) { + if (err) return console.log(err); + console.log('writing to ' + fileName); + }); + }) +}); + // Run elixir tasks elixir(function(mix) { mix.sass('barebones.scss', 'style.css') .scripts(['script.js'], 'js/script.min.js') - .imagemin('/images/**/*', './img'); + .imagemin('/images/**/*', './img') + .hash(); }); diff --git a/header.php b/header.php index fdc1abf..7660712 100755 --- a/header.php +++ b/header.php @@ -7,7 +7,7 @@ - +