11const path = require ( 'path' )
22const fs = require ( 'fs' )
33
4- const getCacheDirs = ( constants , inputs ) => {
5- const getCacheDirs__ = ( constants , input ) => {
6- let cacheDirs = [ ]
4+ const getCacheDirs__ = ( base , input ) => {
5+ let cacheDirs = [ ]
76
8- if ( typeof input === 'string' ) {
9- cacheDirs . push ( path . normalize ( constants . PUBLISH_DIR + '/' + input ) )
10- } else if ( Array . isArray ( input ) ) {
11- input . map ( ( x ) => {
12- cacheDirs . push ( path . normalize ( constants . PUBLISH_DIR + '/' + x ) )
13- } )
14- } else {
15- console . log ( `Warning: Unsupported value in inputs. Ignoring ${ input } ` )
16- }
17-
18- return cacheDirs
7+ if ( typeof input === 'string' ) {
8+ cacheDirs . push ( path . normalize ( base + '/' + input ) )
9+ } else if ( Array . isArray ( input ) ) {
10+ input . map ( ( x ) => {
11+ cacheDirs . push ( path . normalize ( base + '/' + x ) )
12+ } )
13+ } else {
14+ console . log ( `Warning: Unsupported value in inputs. Ignoring ${ input } ` )
1915 }
2016
17+ return cacheDirs
18+ }
19+
20+ const getCacheDirs = ( base , inputs ) => {
2121 return [
22- ...getCacheDirs__ ( constants , inputs . img_cache_local_dir ) ,
23- ...getCacheDirs__ ( constants , inputs . img_cache_remote_dir ) ,
24- ...getCacheDirs__ ( constants , inputs . other_cache_dir ) ,
22+ ...getCacheDirs__ ( base , inputs . img_cache_local_dir ) ,
23+ ...getCacheDirs__ ( base , inputs . img_cache_remote_dir ) ,
24+ ...getCacheDirs__ ( base , inputs . other_cache_dir ) ,
2525 ]
2626}
2727
2828const getHttpHeaders = ( inputs ) => {
2929 let httpHeader = ''
3030
31- inputs . img_cache_local_dir . map ( ( x ) => {
31+ getCacheDirs__ ( '.' , inputs . img_cache_local_dir ) . map ( ( x ) => {
3232 httpHeader += `
3333${ x } /*
3434cache-control: public
@@ -49,11 +49,11 @@ module.exports = {
4949 )
5050 }
5151
52- const cacheDirs = getCacheDirs ( constants , inputs )
52+ const cacheDirs = getCacheDirs ( constants . PUBLISH_DIR , inputs )
5353 cacheDirs . map ( ( x ) => {
5454 if ( fs . existsSync ( x ) ) {
5555 console . log (
56- `Warning: directory ${ x } already exists before restoring caches. It will be replaced.` ,
56+ `Warning: directory ${ x } already exists before restoring caches. It will be replaced if it exists in the cache .` ,
5757 )
5858 }
5959 } )
@@ -69,27 +69,27 @@ module.exports = {
6969 } ,
7070
7171 async onPostBuild ( { constants, inputs, utils } ) {
72- if ( inputs . img_cache_httpHeader ) {
73- fs . appendFile (
74- `${ constants . PUBLISH_DIR } /_headers` ,
75- getHttpHeaders ( inputs ) ,
76- ( err ) => {
77- if ( err ) throw err
78- console . log ( 'Saved http header' )
79- } ,
80- )
81- }
82-
83- const cacheDirs = getCacheDirs ( constants , inputs )
72+ const cacheDirs = getCacheDirs ( constants . PUBLISH_DIR , inputs )
8473
8574 if ( await utils . cache . save ( cacheDirs ) ) {
8675 console . log ( 'Saving 11ty directories to cache:' )
8776 cacheDirs . map ( ( x ) => {
8877 console . log ( '- ' + x )
8978 } )
79+
80+ if ( inputs . img_cache_httpHeader ) {
81+ fs . appendFile (
82+ `${ constants . PUBLISH_DIR } /_headers` ,
83+ getHttpHeaders ( inputs ) ,
84+ ( err ) => {
85+ if ( err ) throw err
86+ console . log ( 'Saved http header' )
87+ } ,
88+ )
89+ }
9090 } else {
9191 console . log (
92- `Warning: 11ty build not found. Is you publish directory set correctly? “${ constants . PUBLISH_DIR } ”` ,
92+ `Warning: Unable to save 11ty cache. Build not found or is empty . Is you publish directory set correctly? “${ constants . PUBLISH_DIR } ”` ,
9393 )
9494 }
9595 } ,
0 commit comments