@@ -55,10 +55,6 @@ class FileCache {
5555 this . #directoryManager. create ( this . #cacheDirectory) ;
5656 }
5757
58- isSideLoaded ( ) {
59- return this . #metadata?. type === "buffer" ;
60- }
61-
6258 set ( type , contents , extraMetadata = { } ) {
6359 this . #savePending = true ;
6460
@@ -76,9 +72,24 @@ class FileCache {
7672 return path . join ( this . #cacheDirectory, this . cacheFilename ) ;
7773 }
7874
75+ getContentsPath ( type ) {
76+ if ( ! type ) {
77+ throw new Error ( "Missing cache type for " + this . fsPath ) ;
78+ }
79+
80+ // normalize to storage type
81+ if ( type === "xml" ) {
82+ type = "text" ;
83+ } else if ( type === "parsed-xml" ) {
84+ type = "json" ;
85+ }
86+
87+ return `${ this . fsPath } .${ type } ` ;
88+ }
89+
7990 // only when side loaded (buffer content)
8091 get contentsPath ( ) {
81- return ` ${ this . fsPath } .buffer` ;
92+ return this . getContentsPath ( this . #metadata ?. type ) ;
8293 }
8394
8495 get ( ) {
@@ -90,11 +101,11 @@ class FileCache {
90101 return ;
91102 }
92103
93- debug ( `Fetching from cache ${ this . contentsPath } ` ) ;
104+ debug ( `Fetching from cache ${ this . fsPath } ` ) ;
94105 if ( this . #source) {
95106 debugAssets ( "[11ty/eleventy-fetch] Reading via %o" , this . #source) ;
96107 } else {
97- debugAssets ( "[11ty/eleventy-fetch] Reading %o" , this . contentsPath ) ;
108+ debugAssets ( "[11ty/eleventy-fetch] Reading %o" , this . fsPath ) ;
98109 }
99110
100111 this . #counts. read ++ ;
@@ -113,10 +124,6 @@ class FileCache {
113124
114125 this . #metadata = json ;
115126
116- if ( json . data ) { // not side-loaded
117- this . #contents = json . data ;
118- }
119-
120127 return json ;
121128 }
122129
@@ -131,30 +138,27 @@ class FileCache {
131138 return Buffer . from ( rawData . contents ) ;
132139 }
133140
134- hasContents ( ) {
141+ hasContents ( type ) {
135142 if ( this . #contents) {
136143 return true ;
137144 }
138- if ( ! this . isSideLoaded ( ) && this . get ( ) ?. data ) {
139- return true ;
140- } else if ( this . get ( ) ?. contents ) {
145+ if ( this . get ( ) ?. contents ) { // backwards compat with very old caches
141146 return true ;
142147 }
143- return existsCache . exists ( this . contentsPath ) ;
148+ return existsCache . exists ( this . getContentsPath ( type ) ) ;
144149 }
145150
146151 getContents ( ) {
147152 if ( this . #contents) {
148153 return this . #contents;
149154 }
150155
151- // Side loaded contents are embedded inside, but we check (for backwards compat)
152- if ( ! this . isSideLoaded ( ) && this . get ( ) ?. data ) {
153- return this . get ( ) ?. data ;
154- } else if ( this . get ( ) ?. contents ) {
155- // backwards compat with old caches
156+ let metadata = this . get ( ) ;
157+ // backwards compat with old caches
158+ if ( metadata ?. contents ) {
159+ // already parsed, part of the top level file
156160 let normalizedContent = this . _backwardsCompatGetContents ( this . get ( ) , this . #metadata. type ) ;
157- this . #contents = normalizedContent ; // set cache
161+ this . #contents = normalizedContent ;
158162 return normalizedContent ;
159163 }
160164
@@ -169,8 +173,13 @@ class FileCache {
169173 debugAssets ( "[11ty/eleventy-fetch] Reading (side loaded) %o" , this . contentsPath ) ;
170174 }
171175
176+ // It is intentional to store contents in a separate file from the metadata: we don’t want to
177+ // have to read the entire contents via JSON.parse (or otherwise) to check the cache validity.
172178 this . #counts. read ++ ;
173179 let data = fs . readFileSync ( this . contentsPath , null ) ;
180+ if ( metadata ?. type === "json" || metadata ?. type === "parsed-xml" ) {
181+ data = JSON . parse ( data ) ;
182+ }
174183 this . #contents = data ;
175184 return data ;
176185 }
@@ -183,28 +192,30 @@ class FileCache {
183192 this . ensureDir ( ) ; // doesn’t add to counts (yet?)
184193
185194 // contents before metadata
186- if ( this . isSideLoaded ( ) ) {
187- debugAssets ( "[11ty/eleventy-fetch] Writing %o (side loaded) from %o" , this . contentsPath , this . #source) ;
188-
189- this . #counts. write ++ ;
195+ debugAssets ( "[11ty/eleventy-fetch] Writing %o (side loaded) from %o" , this . contentsPath , this . #source) ;
190196
191- // the contents must exist before the cache metadata are saved below
192- fs . writeFileSync ( this . contentsPath , this . #contents) ;
193- debug ( `Writing ${ this . contentsPath } ` ) ;
197+ this . #counts. write ++ ;
198+ // the contents must exist before the cache metadata are saved below
199+ let contents = this . #contents;
200+ if ( this . #metadata?. type === "json" || this . #metadata?. type === "parsed-xml" ) {
201+ contents = JSON . stringify ( contents ) ;
194202 }
203+ fs . writeFileSync ( this . contentsPath , contents ) ;
204+ debug ( `Writing ${ this . contentsPath } ` ) ;
195205
196206 this . #counts. write ++ ;
197207 debugAssets ( "[11ty/eleventy-fetch] Writing %o from %o" , this . fsPath , this . #source) ;
198- fs . writeFileSync ( this . fsPath , JSON . stringify ( Object . assign ( { } , this . #metadata, { data : this . #contents } ) ) , "utf8" ) ;
208+ fs . writeFileSync ( this . fsPath , JSON . stringify ( this . #metadata) , "utf8" ) ;
199209 debug ( `Writing ${ this . fsPath } ` ) ;
200210 }
201211
202212 // for testing
203- getFilePaths ( ) {
213+ getAllPossibleFilePaths ( ) {
214+ let types = [ "text" , "buffer" , "json" ] ;
204215 let paths = new Set ( ) ;
205216 paths . add ( this . fsPath ) ;
206- if ( this . isSideLoaded ( ) ) {
207- paths . add ( this . contentsPath ) ;
217+ for ( let type of types ) {
218+ paths . add ( this . getContentsPath ( type ) ) ;
208219 }
209220 return Array . from ( paths ) ;
210221 }
0 commit comments