UNPKG

@greychrist/react-cache-buster

Version:

This package allows clients to automatically check the new version or git commit hash when a new version is released in the production environment, and if a new version is published, clearing the cache and reload the page.

30 lines (24 loc) 867 B
var _require = require('fs'), writeFile = _require.writeFile; var path = require('path'); var packageJson = require(process.cwd() + "/package.json"); var metaJson = path.join(process.cwd(), 'public', 'meta.json'); var appVersion = 'no-version-found'; var gitHash = 'no-git-hash-found'; try { appVersion = packageJson.version; gitHash = require('child_process').execSync('git rev-parse HEAD').toString().trim(); } catch (ex) {} var jsonData = { version: appVersion, hash: gitHash }; var jsonContent = JSON.stringify(jsonData); writeFile(metaJson, jsonContent, 'utf8', function (err) { if (err) { console.error('CacheBuster: An error occurred while writing JSON Object to meta.json'); throw console.error(err); } else { console.log("CacheBuster: meta.json file has been saved with v" + appVersion + " and gitHash: " + gitHash); } });