Skip to content

Commit 6be60ec

Browse files
committed
gatsby node
1 parent bd0a703 commit 6be60ec

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

gatsby-node.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
const path = require(`path`)
22
const { postsPerPage } = require(`./src/utils/siteConfig`)
33
const { paginate } = require(`gatsby-awesome-pagination`)
4+
const util = require('util');
5+
const glob = require("glob")
6+
const fs = require('fs');
7+
const md5 = require('md5');
8+
const hash = md5(`${new Date().getTime()}`)
9+
10+
const addPageDataVersion = async file => {
11+
const stats = await util.promisify(fs.stat)(file)
12+
if (stats.isFile()) {
13+
console.log(`Adding version to page-data.json in ${file}..`)
14+
let content = await util.promisify(fs.readFile)(file, 'utf8')
15+
const result = content.replace(
16+
/page-data.json(\?v=[a-f0-9]{32})?/g,
17+
`page-data.json?v=${hash}`
18+
)
19+
await util.promisify(fs.writeFile)(file, result, 'utf8')
20+
}
21+
}
22+
23+
exports.onPostBootstrap = async () => {
24+
const loader = path.join(__dirname, 'node_modules/gatsby/cache-dir/loader.js')
25+
await addPageDataVersion(loader)
26+
}
27+
28+
exports.onPostBuild = async () => {
29+
const publicPath = path.join(__dirname, 'public')
30+
const htmlAndJSFiles = glob.sync(`${publicPath}/**/*.{html,js}`)
31+
for (let file of htmlAndJSFiles) {
32+
await addPageDataVersion(file)
33+
}
34+
}
435

536
/**
637
* Here is the place where Gatsby creates the URLs for all the
@@ -159,4 +190,4 @@ exports.createPages = async ({ graphql, actions }) => {
159190
}
160191
},
161192
})
162-
}
193+
}

0 commit comments

Comments
 (0)