11const _ = require ( 'lodash' ) ;
22const chalk = require ( 'chalk' ) ;
3+ const crypto = require ( 'crypto' ) ;
34const fm = require ( 'json-front-matter' ) ;
45const fs = require ( 'fs' ) ;
56const glob = require ( 'glob' ) ;
@@ -33,18 +34,27 @@ module.exports = globPattern => new Promise((resolve, reject) => {
3334 reject ( err ) ;
3435 }
3536
37+ const npmPackage = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf8' ) ) ;
38+
3639 console . log ( '- Found' , components . length , 'components' ) ;
3740
3841 const componentsForNav = { } ;
3942 components . forEach ( ( component ) => {
4043 const componentTokens = component . replace ( 'src/components/' , '' ) . split ( '/' ) ;
4144 const category = componentTokens [ 0 ] ;
4245
43- // Check the front matter for screenshot overrides
4446 const componentHtml = fs . readFileSync ( component , 'utf8' ) ;
4547 const fmParsed = fm . parse ( componentHtml ) ;
4648 const frontMatter = fmParsed . attributes || { } ;
4749 const dir = component . replace ( 'src/' , '' ) . replace ( '.html' , '' ) ;
50+
51+ // Compute component signature based on the Tachyons version and the contents of the
52+ // component itself. This can be used to bust the browser cache of screenshots.
53+ const md5sum = crypto . createHash ( 'md5' ) ;
54+ md5sum . update ( npmPackage . version ) ;
55+ md5sum . update ( componentHtml ) ;
56+ const signature = md5sum . digest ( 'hex' ) ;
57+
4858 componentsForNav [ category ] = componentsForNav [ category ] || [ ] ;
4959 componentsForNav [ category ] . push ( {
5060 name : frontMatter . name || getName ( component ) ,
@@ -54,8 +64,9 @@ module.exports = globPattern => new Promise((resolve, reject) => {
5464 href : `/${ dir } /index.html` ,
5565 screenshot : {
5666 path : `${ dir } /${ screenshotBasename } .png` ,
57- href : `/${ dir } /${ screenshotBasename } .png` ,
67+ href : `/${ dir } /${ screenshotBasename } .png?version= ${ signature } ` ,
5868 } ,
69+ signature,
5970 frontMatter,
6071 } ) ;
6172 } ) ;
0 commit comments