Skip to content

Commit d17367b

Browse files
add component hash signature to cache bust screenshots
1 parent 9ce37b7 commit d17367b

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const componentsBuildIndex = require('./src/components-build-index');
1616
const componentsBuildPages = require('./src/components-build-pages');
1717
const componentsBuildScreenshots = require('./src/components-build-screenshots');
1818

19-
const globPattern = 'src/components/cards/*.html';
19+
const globPattern = 'src/components/banners/*.html';
2020
componentsBuildIndex(globPattern)
2121
.then(componentsForNavPath => componentsBuildPages(componentsForNavPath))
2222
.then(componentsForNavPath => componentsBuildScreenshots(componentsForNavPath))

src/components-build-index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require('lodash');
22
const chalk = require('chalk');
3+
const crypto = require('crypto');
34
const fm = require('json-front-matter');
45
const fs = require('fs');
56
const 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
});

src/components-build-pages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = componentsForNavPath => new Promise((resolve, reject) => {
9494
const compiledComponent = _.template(componentTemplate)(frontMatter);
9595
mkdirp.sync(path.dirname(component.path));
9696
fs.writeFileSync(component.path, compiledComponent);
97-
console.log(' * Created component:', component.path);
97+
console.log(' * Created component:', component.href);
9898
}).catch((e) => {
9999
console.log(e);
100100
});

src/components-build-screenshots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ module.exports = componentsForNavPath => new Promise((resolve, reject) => {
8787
// do *not* use componentRect in .screenshot() below or risk distortions
8888
.screenshot(component.screenshot.path)
8989
.then(() => {
90-
console.log(' * Created screenshot: ', component.screenshot.path);
90+
console.log(' * Created screenshot: ', component.screenshot.href);
9191
});
9292
}
9393
} // Loop over components

0 commit comments

Comments
 (0)