@@ -86,27 +86,25 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
8686 const buildId = buildHelper . getBuildId ( options ) ;
8787 let useTagCache = false ;
8888
89- // Copy pages to cache folder
9089 const dotNextPath = path . join (
9190 appBuildOutputPath ,
9291 ".next/standalone" ,
9392 packagePath ,
9493 ) ;
95- const outputPath = path . join ( outputDir , "cache" , buildId ) ;
96- [ ".next/server/pages" , ".next/server/app" ]
94+
95+ const outputCachePath = path . join ( outputDir , "cache" , buildId ) ;
96+ fs . mkdirSync ( outputCachePath , { recursive : true } ) ;
97+
98+ const sourceDirs = [ ".next/server/pages" , ".next/server/app" ]
9799 . map ( ( dir ) => path . join ( dotNextPath , dir ) )
98- . filter ( fs . existsSync )
99- . forEach ( ( dir ) => fs . cpSync ( dir , outputPath , { recursive : true } ) ) ;
100+ . filter ( fs . existsSync ) ;
100101
101- // Remove non-cache files
102102 const htmlPages = buildHelper . getHtmlPages ( dotNextPath ) ;
103- buildHelper . removeFiles (
104- outputPath ,
105- ( { relativePath } ) =>
106- relativePath . endsWith ( ".js" ) ||
107- relativePath . endsWith ( ".js.nft.json" ) ||
108- ( relativePath . endsWith ( ".html" ) && htmlPages . has ( relativePath ) ) ,
109- ) ;
103+
104+ const isFileSkipped = ( relativePath : string ) =>
105+ relativePath . endsWith ( ".js" ) ||
106+ relativePath . endsWith ( ".js.nft.json" ) ||
107+ ( relativePath . endsWith ( ".html" ) && htmlPages . has ( relativePath ) ) ;
110108
111109 // Merge cache files into a single file
112110 const cacheFilesPath : Record <
@@ -120,36 +118,43 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
120118 }
121119 > = { } ;
122120
123- buildHelper . traverseFiles (
124- outputPath ,
125- ( ) => true ,
126- ( { absolutePath } ) => {
127- const ext = path . extname ( absolutePath ) ;
128- switch ( ext ) {
129- case ".meta" :
130- case ".html" :
131- case ".json" :
132- case ".body" :
133- case ".rsc" : {
134- const newFilePath = absolutePath
135- . substring ( 0 , absolutePath . length - ext . length )
136- . replace ( / \. p r e f e t c h $ / , "" )
137- . concat ( ".cache" ) ;
121+ // Process each source directory
122+ sourceDirs . forEach ( ( sourceDir ) => {
123+ buildHelper . traverseFiles (
124+ sourceDir ,
125+ ( { relativePath } ) => ! isFileSkipped ( relativePath ) ,
126+ ( { absolutePath, relativePath } ) => {
127+ const ext = path . extname ( absolutePath ) ;
128+ switch ( ext ) {
129+ case ".meta" :
130+ case ".html" :
131+ case ".json" :
132+ case ".body" :
133+ case ".rsc" : {
134+ const newFilePath = path
135+ . join ( outputCachePath , relativePath )
136+ . substring (
137+ 0 ,
138+ path . join ( outputCachePath , relativePath ) . length - ext . length ,
139+ )
140+ . replace ( / \. p r e f e t c h $ / , "" )
141+ . concat ( ".cache" ) ;
138142
139- cacheFilesPath [ newFilePath ] = {
140- [ ext . slice ( 1 ) ] : absolutePath ,
141- ...cacheFilesPath [ newFilePath ] ,
142- } ;
143- break ;
143+ cacheFilesPath [ newFilePath ] = {
144+ [ ext . slice ( 1 ) ] : absolutePath ,
145+ ...cacheFilesPath [ newFilePath ] ,
146+ } ;
147+ break ;
148+ }
149+ case ".map" :
150+ break ;
151+ default :
152+ logger . warn ( `Unknown file extension: ${ ext } ` ) ;
153+ break ;
144154 }
145- case ".map" :
146- break ;
147- default :
148- logger . warn ( `Unknown file extension: ${ ext } ` ) ;
149- break ;
150- }
151- } ,
152- ) ;
155+ } ,
156+ ) ;
157+ } ) ;
153158
154159 // Generate cache file
155160 Object . entries ( cacheFilesPath ) . forEach ( ( [ cacheFilePath , files ] ) => {
@@ -174,6 +179,9 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
174179 )
175180 : undefined ,
176181 } ;
182+
183+ // Ensure directory exists before writing
184+ fs . mkdirSync ( path . dirname ( cacheFilePath ) , { recursive : true } ) ;
177185 fs . writeFileSync ( cacheFilePath , JSON . stringify ( cacheFileContent ) ) ;
178186 } ) ;
179187
@@ -212,38 +220,41 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
212220 if ( ! options . config . dangerous ?. disableTagCache ) {
213221 // Compute dynamodb cache data
214222 // Traverse files inside cache to find all meta files and cache tags associated with them
215- buildHelper . traverseFiles (
216- outputPath ,
217- ( { absolutePath } ) => absolutePath . endsWith ( ".meta" ) ,
218- ( { absolutePath, relativePath } ) => {
219- const fileContent = fs . readFileSync ( absolutePath , "utf8" ) ;
220- const fileData = JSON . parse ( fileContent ) ;
221- if ( fileData . headers ?. [ "x-next-cache-tags" ] ) {
222- fileData . headers [ "x-next-cache-tags" ]
223- . split ( "," )
224- . forEach ( ( tag : string ) => {
225- // TODO: We should split the tag using getDerivedTags from next.js or maybe use an in house implementation
226- metaFiles . push ( {
227- tag : { S : path . posix . join ( buildId , tag . trim ( ) ) } ,
228- path : {
229- S : path . posix . join (
230- buildId ,
231- relativePath . replace ( ".meta" , "" ) ,
232- ) ,
233- } ,
234- // We don't care about the revalidation time here, we just need to make sure it's there
235- revalidatedAt : { N : "1" } ,
223+ sourceDirs . forEach ( ( sourceDir ) => {
224+ buildHelper . traverseFiles (
225+ sourceDir ,
226+ ( { absolutePath, relativePath } ) =>
227+ absolutePath . endsWith ( ".meta" ) && ! isFileSkipped ( relativePath ) ,
228+ ( { absolutePath, relativePath } ) => {
229+ const fileContent = fs . readFileSync ( absolutePath , "utf8" ) ;
230+ const fileData = JSON . parse ( fileContent ) ;
231+ if ( fileData . headers ?. [ "x-next-cache-tags" ] ) {
232+ fileData . headers [ "x-next-cache-tags" ]
233+ . split ( "," )
234+ . forEach ( ( tag : string ) => {
235+ // TODO: We should split the tag using getDerivedTags from next.js or maybe use an in house implementation
236+ metaFiles . push ( {
237+ tag : { S : path . posix . join ( buildId , tag . trim ( ) ) } ,
238+ path : {
239+ S : path . posix . join (
240+ buildId ,
241+ relativePath . replace ( ".meta" , "" ) ,
242+ ) ,
243+ } ,
244+ // We don't care about the revalidation time here, we just need to make sure it's there
245+ revalidatedAt : { N : "1" } ,
246+ } ) ;
236247 } ) ;
237- } ) ;
238- }
239- } ,
240- ) ;
248+ }
249+ } ,
250+ ) ;
251+ } ) ;
241252
242253 if ( metaFiles . length > 0 ) {
243254 useTagCache = true ;
244255 const providerPath = path . join ( outputDir , "dynamodb-provider" ) ;
245256
246- //Copy open-next.config.mjs into the bundle
257+ // Copy open-next.config.mjs into the bundle
247258 fs . mkdirSync ( providerPath , { recursive : true } ) ;
248259 buildHelper . copyOpenNextConfig ( options . buildDir , providerPath ) ;
249260
@@ -255,11 +266,5 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
255266 }
256267 }
257268
258- // We need to remove files later because we need the metafiles for dynamodb tags cache
259- buildHelper . removeFiles (
260- outputPath ,
261- ( { relativePath } ) => ! relativePath . endsWith ( ".cache" ) ,
262- ) ;
263-
264269 return { useTagCache, metaFiles } ;
265270}
0 commit comments