1
1
const path = require ( `path` )
2
2
const { postsPerPage } = require ( `./src/utils/siteConfig` )
3
3
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
+ / p a g e - d a t a .j s o n ( \? v = [ a - f 0 - 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
+ }
4
35
5
36
/**
6
37
* Here is the place where Gatsby creates the URLs for all the
@@ -159,4 +190,4 @@ exports.createPages = async ({ graphql, actions }) => {
159
190
}
160
191
} ,
161
192
} )
162
- }
193
+ }
0 commit comments