Last active
August 29, 2015 14:20
-
-
Save Kevinlearynet/9653f9f42496db1cbc64 to your computer and use it in GitHub Desktop.
Hapi.js plugin to autoversion files based on when they last changed: `/file.js?v={mtime_timestamp}`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * DFP Test Page Controller | |
| * | |
| * This pulls in the data we need to output | |
| * a story URL | |
| * | |
| * @pattern /dfp/ | |
| */ | |
| 'use strict'; | |
| module.exports = { | |
| id: 'dfp', | |
| handler: function ( request, reply ) { | |
| reply.view( 'entry_dfp', { | |
| title: 'DFP Ad-units', | |
| desc: 'Changing ads when the viewport scrolls beyond a specific point.', | |
| assets: { | |
| dev: { | |
| css: [ { | |
| file: '/_/dist/css/view-dfp.dev.css' | |
| } ] | |
| }, | |
| prod: { | |
| css: [ { | |
| file: '/_/dist/css/view-dfp.prod.css' | |
| } ] | |
| } | |
| } | |
| } ); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * mtime based asset versioning | |
| * | |
| * Autoversion files based on when they last changed: | |
| * /file.js?v={mtime_timestamp} | |
| */ | |
| 'use strict'; | |
| var _ = require( 'lodash' ), | |
| is = require( 'is_js' ), | |
| fs = require( 'fs' ); | |
| exports.register = function ( server, options, next ) { | |
| var environment = global.__config.env; | |
| // hook: 'onPostHandler' | |
| server.ext( 'onPostHandler', function ( request, reply ) { | |
| var response = request.response; | |
| // must be view | |
| if ( response.variety !== 'view' ) | |
| return reply.continue(); | |
| // defaults if empty | |
| if ( is.not.existy( response.source.context ) ) { | |
| response.source.context = {}; | |
| } | |
| if ( is.not.existy( response.source.context.assets ) ) { | |
| response.source.context.assets = {}; | |
| } | |
| // global css/js | |
| var globalAssets = options[ environment ]; | |
| var localAssets = response.source.context.assets[ environment ]; | |
| // merge controller CSS/JS with globals | |
| if ( is.existy( localAssets ) && is.not.empty( localAssets ) ) { | |
| // css | |
| if ( is.existy( localAssets.css ) ) { | |
| globalAssets.css = _.union( globalAssets.css, localAssets.css ); | |
| } | |
| // js | |
| if ( is.existy( localAssets.js ) ) { | |
| globalAssets.js = _.union( globalAssets.js, localAssets.js ); | |
| } | |
| } | |
| console.log( globalAssets ); | |
| // mtime for each asset | |
| _.each( globalAssets, function ( files, type ) { | |
| _.each( files, function ( obj, key ) { | |
| // skip versioning? | |
| if ( _.has( obj, 'version' ) && obj.version === false ) | |
| return; | |
| // file last modified timestamp | |
| var absLoc = obj.file.replace( '/_/', '/public/' ); | |
| var filePath = require.main.filename.replace( 'server.js', absLoc ); | |
| var mtime = new Date( fs.statSync( filePath ).mtime ).getTime() / 1000; | |
| // add property | |
| globalAssets[ type ][ key ].version = mtime; | |
| } ); | |
| } ); | |
| // add final assets obj to context | |
| response.source.context.assets = globalAssets; | |
| return reply.continue(); | |
| } ); | |
| return next(); | |
| }; | |
| /** | |
| * Plugin metadata | |
| */ | |
| exports.register.attributes = { | |
| name: 'mittr-mtime-assets', | |
| version: '0.0.1' | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment