-
Notifications
You must be signed in to change notification settings - Fork 19
/
gh-pages-cache.js
57 lines (56 loc) · 2.23 KB
/
gh-pages-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const { v4: md5 } = require('uuid')
// Work around for caches on gh-pages
// https://github.com/gatsbyjs/gatsby/issues/15080#issuecomment-765338035
const publicPath = path.join(__dirname, 'public')
const publicCachePath = path.join(__dirname, 'public-cache')
if (fs.existsSync(publicCachePath)) {
fs.rmdirSync(publicCachePath, { recursive: true })
}
fs.cpSync(publicPath, publicCachePath, { recursive: true })
console.log(`[onPostBuild] Copied ${publicPath} to ${publicCachePath}`)
const hash = md5(Math.random().toString(36).substring(7))
const jsonFiles = glob.sync(`${publicPath}/page-data/**/page-data.json`)
console.log(`[onPostBuild] Renaming the following files to page-data.${hash}.json:`)
for (const file of jsonFiles) {
console.log(file)
const newFilename = file.replace('page-data.json', `page-data.${hash}.json`)
fs.renameSync(file, newFilename)
}
const appShaFiles = glob.sync(`${publicPath}/**/app-+([^-]).js`)
const [appShaFile] = appShaFiles
const [appShaFilename] = appShaFile.split('/').slice(-1)
const appShaFilenameReg = new RegExp(appShaFilename, 'g')
const newAppShaFilename = `app-${hash}.js`
const newFilePath = appShaFile.replace(appShaFilename, newAppShaFilename)
console.log(`[onPostBuild] Renaming: ${appShaFilename} to ${newAppShaFilename}`)
fs.renameSync(appShaFile, newFilePath)
if (fs.existsSync(`${appShaFile}.map`)) {
fs.renameSync(`${appShaFile}.map`, `${newFilePath}.map`)
}
if (fs.existsSync(`${appShaFile}.LICENSE.txt`)) {
fs.renameSync(`${appShaFile}.LICENSE.txt`, `${newFilePath}.LICENSE.txt`)
}
const htmlJSAndJSONFiles = [
`${newFilePath}.map`,
...glob.sync(`${publicPath}/**/*.{html,js,json}`)
]
console.log(
`[onPostBuild] Replacing page-data.json, ${appShaFilename}, and ${appShaFilename}.map references in the following files:`
)
for (const file of htmlJSAndJSONFiles) {
const stats = fs.statSync(file, 'utf8')
if (!stats.isFile()) {
continue
}
const content = fs.readFileSync(file, 'utf8')
const result = content
.replace(appShaFilenameReg, newAppShaFilename)
.replace(/page-data.json/g, `page-data.${hash}.json`)
if (result !== content) {
console.log(file)
fs.writeFileSync(file, result, 'utf8')
}
}