Skip to content

Commit

Permalink
- added cache busting
Browse files Browse the repository at this point in the history
  • Loading branch information
dshuffma-ibm committed Jul 21, 2016
1 parent f18131b commit 8c56bd5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions busters_css.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"public/css/singlecsshash":"5c5213ec16de93e801c4aaf7a7eb956b"}
1 change: 1 addition & 0 deletions busters_js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"public/js/singlejshash":"95dd01ace4ead82e74147c60e9913998"}
21 changes: 18 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var sass = require('gulp-sass');
var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var rename = require('gulp-rename');
var bust = require('gulp-buster');
var spawn = require('child_process').spawn;
var node;

Expand All @@ -17,20 +18,34 @@ gulp.task('build-sass', function () {
.pipe(gulp.dest(path.join(__dirname, '/public/css')))
.pipe(cleanCSS()) //minify
.pipe(rename('main.min.css'))
.pipe(gulp.dest(path.join(__dirname,'/public/css'))); //dump it here
.pipe(gulp.dest(path.join(__dirname,'/public/css'))) //dump it here
.pipe(rename('singlecsshash'))
.pipe(bust({fileName: 'busters_css.json'})) //cache bust
.pipe(gulp.dest('.')); //dump busters_css.json
});

gulp.task('build-js-hash', function () {
gulp.src(path.join(__dirname,'/public/js/*.js'))
.pipe(concat('singlejshash')) //concat them all
.pipe(bust({fileName: 'busters_js.json'})) //cache bust
.pipe(gulp.dest('.')); //dump busters_js.json
});

////// Run Server Task ///////
gulp.task('server', function() {
if(node) node.kill();
node = spawn('node', ['app.js'], {stdio: 'inherit'}); //command, file, options
node = spawn('node', ['app.js'], {stdio: 'inherit'}); //command, file, options
});

////// Watch Tasks //////
gulp.task('watch-sass', ['build-sass'], function () {
gulp.watch(path.join(__dirname, '/scss/*.scss'), ['build-sass']);
});

gulp.task('watch-js', ['build-js-hash'], function () {
gulp.watch(path.join(__dirname,'/public/js/*.js'), ['build-js-hash']);
});

gulp.task('watch-server', ['server'], function () {
gulp.watch(path.join(__dirname, '/routes/**/*.js'), ['server']);
gulp.watch([path.join(__dirname, '/utils/**/*.js')], ['server']);
Expand All @@ -39,4 +54,4 @@ gulp.task('watch-server', ['server'], function () {
});

////// Tasks //////
gulp.task('default', ['watch-sass', 'watch-server']);
gulp.task('default', ['watch-sass', 'watch-js', 'watch-server']);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"gulp-sass": "^2.0.4",
"gulp-concat": "*",
"gulp-clean-css": "*",
"gulp-rename": "*"
"gulp-rename": "*",
"gulp-buster": "*"
},
"author": {
"name": "David Huffman"
Expand Down
18 changes: 14 additions & 4 deletions routes/site_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ var express = require('express');
var router = express.Router();
var setup = require('../setup.js');

//anything in here gets passed to JADE template engine
function build_bag(){
return {
setup: setup, //static vars for configuration settings
e: process.error, //send any setup errors
jshash: process.env.cachebust_js, //js cache busting hash (not important)
csshash: process.env.cachebust_css, //css cache busting hash (not important)
};
}

// ============================================================================================================================
// Home
// ============================================================================================================================
Expand All @@ -23,20 +33,20 @@ router.route('/').get(function(req, res){
// Part 1
// ============================================================================================================================
router.route('/p1').get(function(req, res){
res.render('part1', {title: 'Marbles Part 1', bag: {setup: setup, e: process.error}} );
res.render('part1', {title: 'Marbles Part 1', bag: build_bag()});
});
router.route('/p1/:page?').get(function(req, res){
res.render('part1', {title: 'Marbles Part 1', bag: {setup: setup, e: process.error}} );
res.render('part1', {title: 'Marbles Part 1', bag: build_bag()});
});

// ============================================================================================================================
// Part 2
// ============================================================================================================================
router.route('/p2').get(function(req, res){
res.render('part2', {title: 'Marbles Part 2', bag: {setup: setup, e: process.error}} );
res.render('part2', {title: 'Marbles Part 2', bag: build_bag()});
});
router.route('/p2/:page?').get(function(req, res){
res.render('part2', {title: 'Marbles Part 2', bag: {setup: setup, e: process.error}} );
res.render('part2', {title: 'Marbles Part 2', bag: build_bag()});
});

module.exports = router;
2 changes: 1 addition & 1 deletion views/part1.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends ./template/layout.jade

// --------------- Header -------------- //
block custom_header
script(src='/js/part1.js')
script(src='/js/part1.js?v=#{bag.jshash}')
script(type='text/javascript').
$(document).ready(function(){

Expand Down
4 changes: 2 additions & 2 deletions views/part2.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extends ./template/layout.jade

// --------------- Header -------------- //
block custom_header
script(src='/js/part2.js')
script(src='/js/blockchain.js')
script(src='/js/part2.js?v=#{bag.jshash}')
script(src='/js/blockchain.js?v=#{bag.jshash}')
script(type='text/javascript').
$(document).ready(function(){

Expand Down
2 changes: 1 addition & 1 deletion views/template/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ html
meta(name="viewport" content="initial-scale=1.0, user-scalable=no")
link(rel="icon", type="image/png", href="/imgs/favicon.ico")
link(rel="stylesheet" href="/css/font-awesome-4.5.0/css/font-awesome.min.css")
link(type="text/css" rel="stylesheet" href="/css/main.min.css")
link(type="text/css" rel="stylesheet" href="/css/main.min.css?v=#{bag.csshash}")
script(src='/js/util/jquery-1.11.1.min.js')
script(src='/js/util/jquery-ui-1.11.4/jquery-ui.min.js')
script(src='/js/util/jquery.ui.touch-punch.min.js')
Expand Down

0 comments on commit 8c56bd5

Please sign in to comment.