@@ -13,6 +13,19 @@ import {
13
13
import { minifyAll } from "./minimize-js.js" ;
14
14
import openNextPlugin from "./plugin.js" ;
15
15
16
+ interface DangerousOptions {
17
+ /**
18
+ * The dynamo db cache is used for revalidateTags and revalidatePath.
19
+ * @default false
20
+ */
21
+ disableDynamoDBCache ?: boolean ;
22
+ /**
23
+ * The incremental cache is used for ISR and SSG.
24
+ * Disable this only if you use only SSR
25
+ * @default false
26
+ */
27
+ disableIncrementalCache ?: boolean ;
28
+ }
16
29
interface BuildOptions {
17
30
/**
18
31
* Minify the server bundle.
@@ -39,6 +52,10 @@ interface BuildOptions {
39
52
* });
40
53
* ```
41
54
*/
55
+ /**
56
+ * Dangerous options. This break some functionnality but can be useful in some cases.
57
+ */
58
+ dangerous ?: DangerousOptions ;
42
59
buildCommand ?: string ;
43
60
/**
44
61
* The path to the target folder of build output from the `buildCommand` option (the path which will contain the `.next` and `.open-next` folders). This path is relative from the current process.cwd().
@@ -82,8 +99,10 @@ export async function build(opts: BuildOptions = {}) {
82
99
printHeader ( "Generating bundle" ) ;
83
100
initOutputDir ( ) ;
84
101
createStaticAssets ( ) ;
85
- createCacheAssets ( monorepoRoot ) ;
86
- await createServerBundle ( monorepoRoot , opts . streaming ) ;
102
+ if ( ! options . dangerous ?. disableIncrementalCache ) {
103
+ createCacheAssets ( monorepoRoot , options . dangerous ?. disableDynamoDBCache ) ;
104
+ }
105
+ await createServerBundle ( monorepoRoot , options . streaming ) ;
87
106
createRevalidationBundle ( ) ;
88
107
createImageOptimizationBundle ( ) ;
89
108
createWarmerBundle ( ) ;
@@ -109,6 +128,8 @@ function normalizeOptions(opts: BuildOptions, root: string) {
109
128
minify : opts . minify ?? Boolean ( process . env . OPEN_NEXT_MINIFY ) ?? false ,
110
129
debug : opts . debug ?? Boolean ( process . env . OPEN_NEXT_DEBUG ) ?? false ,
111
130
buildCommand : opts . buildCommand ,
131
+ dangerous : opts . dangerous ,
132
+ streaming : opts . streaming ?? false ,
112
133
} ;
113
134
}
114
135
@@ -369,7 +390,7 @@ function createStaticAssets() {
369
390
}
370
391
}
371
392
372
- function createCacheAssets ( monorepoRoot : string ) {
393
+ function createCacheAssets ( monorepoRoot : string , disableDynamoDBCache = false ) {
373
394
console . info ( `Bundling cache assets...` ) ;
374
395
375
396
const { appBuildOutputPath, outputDir } = options ;
@@ -398,23 +419,14 @@ function createCacheAssets(monorepoRoot: string) {
398
419
( file . endsWith ( ".html" ) && htmlPages . has ( file ) ) ,
399
420
) ;
400
421
401
- // Generate dynamodb data
402
- // We need to traverse the cache to find every .meta file
403
- const metaFiles : {
404
- tag : { S : string } ;
405
- path : { S : string } ;
406
- revalidatedAt : { N : string } ;
407
- } [ ] = [ ] ;
408
-
409
- // Copy fetch-cache to cache folder
410
- const fetchCachePath = path . join (
411
- appBuildOutputPath ,
412
- ".next/cache/fetch-cache" ,
413
- ) ;
414
- if ( fs . existsSync ( fetchCachePath ) ) {
415
- const fetchOutputPath = path . join ( outputDir , "cache" , "__fetch" , buildId ) ;
416
- fs . mkdirSync ( fetchOutputPath , { recursive : true } ) ;
417
- fs . cpSync ( fetchCachePath , fetchOutputPath , { recursive : true } ) ;
422
+ if ( ! disableDynamoDBCache ) {
423
+ // Generate dynamodb data
424
+ // We need to traverse the cache to find every .meta file
425
+ const metaFiles : {
426
+ tag : { S : string } ;
427
+ path : { S : string } ;
428
+ revalidatedAt : { N : string } ;
429
+ } [ ] = [ ] ;
418
430
419
431
// Compute dynamodb cache data
420
432
// Traverse files inside cache to find all meta files and cache tags associated with them
@@ -444,41 +456,54 @@ function createCacheAssets(monorepoRoot: string) {
444
456
} ,
445
457
) ;
446
458
447
- traverseFiles (
448
- fetchCachePath ,
449
- ( ) => true ,
450
- ( filepath ) => {
451
- const fileContent = fs . readFileSync ( filepath , "utf8" ) ;
452
- const fileData = JSON . parse ( fileContent ) ;
453
- fileData ?. tags ?. forEach ( ( tag : string ) => {
454
- metaFiles . push ( {
455
- tag : { S : path . posix . join ( buildId , tag ) } ,
456
- path : {
457
- S : path . posix . join (
458
- buildId ,
459
- path . relative ( fetchCachePath , filepath ) ,
460
- ) ,
461
- } ,
462
- revalidatedAt : { N : `${ Date . now ( ) } ` } ,
463
- } ) ;
464
- } ) ;
465
- } ,
459
+ // Copy fetch-cache to cache folder
460
+ const fetchCachePath = path . join (
461
+ appBuildOutputPath ,
462
+ ".next/cache/fetch-cache" ,
466
463
) ;
464
+ if ( fs . existsSync ( fetchCachePath ) ) {
465
+ const fetchOutputPath = path . join ( outputDir , "cache" , "__fetch" , buildId ) ;
466
+ fs . mkdirSync ( fetchOutputPath , { recursive : true } ) ;
467
+ fs . cpSync ( fetchCachePath , fetchOutputPath , { recursive : true } ) ;
467
468
468
- const providerPath = path . join ( outputDir , "dynamodb-provider" ) ;
469
+ traverseFiles (
470
+ fetchCachePath ,
471
+ ( ) => true ,
472
+ ( filepath ) => {
473
+ const fileContent = fs . readFileSync ( filepath , "utf8" ) ;
474
+ const fileData = JSON . parse ( fileContent ) ;
475
+ fileData ?. tags ?. forEach ( ( tag : string ) => {
476
+ metaFiles . push ( {
477
+ tag : { S : path . posix . join ( buildId , tag ) } ,
478
+ path : {
479
+ S : path . posix . join (
480
+ buildId ,
481
+ path . relative ( fetchCachePath , filepath ) ,
482
+ ) ,
483
+ } ,
484
+ revalidatedAt : { N : `${ Date . now ( ) } ` } ,
485
+ } ) ;
486
+ } ) ;
487
+ } ,
488
+ ) ;
489
+ }
469
490
470
- esbuildSync ( {
471
- external : [ "@aws-sdk/client-dynamodb" ] ,
472
- entryPoints : [ path . join ( __dirname , "adapters" , "dynamo-provider.js" ) ] ,
473
- outfile : path . join ( providerPath , "index.mjs" ) ,
474
- target : [ "node18" ] ,
475
- } ) ;
491
+ if ( metaFiles . length > 0 ) {
492
+ const providerPath = path . join ( outputDir , "dynamodb-provider" ) ;
476
493
477
- // TODO: check if metafiles doesn't contain duplicates
478
- fs . writeFileSync (
479
- path . join ( providerPath , "dynamodb-cache.json" ) ,
480
- JSON . stringify ( metaFiles ) ,
481
- ) ;
494
+ esbuildSync ( {
495
+ external : [ "@aws-sdk/client-dynamodb" ] ,
496
+ entryPoints : [ path . join ( __dirname , "adapters" , "dynamo-provider.js" ) ] ,
497
+ outfile : path . join ( providerPath , "index.mjs" ) ,
498
+ target : [ "node18" ] ,
499
+ } ) ;
500
+
501
+ // TODO: check if metafiles doesn't contain duplicates
502
+ fs . writeFileSync (
503
+ path . join ( providerPath , "dynamodb-cache.json" ) ,
504
+ JSON . stringify ( metaFiles ) ,
505
+ ) ;
506
+ }
482
507
}
483
508
}
484
509
@@ -602,7 +627,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
602
627
addPublicFilesList ( outputPath , packagePath ) ;
603
628
injectMiddlewareGeolocation ( outputPath , packagePath ) ;
604
629
removeCachedPages ( outputPath , packagePath ) ;
605
- addCacheHandler ( outputPath ) ;
630
+ addCacheHandler ( outputPath , options . dangerous ) ;
606
631
}
607
632
608
633
function addMonorepoEntrypoint ( outputPath : string , packagePath : string ) {
@@ -710,13 +735,20 @@ function removeCachedPages(outputPath: string, packagePath: string) {
710
735
) ;
711
736
}
712
737
713
- function addCacheHandler ( outputPath : string ) {
738
+ function addCacheHandler ( outputPath : string , options ?: DangerousOptions ) {
714
739
esbuildSync ( {
715
740
external : [ "next" , "styled-jsx" , "react" ] ,
716
741
entryPoints : [ path . join ( __dirname , "adapters" , "cache.js" ) ] ,
717
742
outfile : path . join ( outputPath , "cache.cjs" ) ,
718
743
target : [ "node18" ] ,
719
744
format : "cjs" ,
745
+ banner : {
746
+ js : `globalThis.disableIncrementalCache = ${
747
+ options ?. disableIncrementalCache ?? false
748
+ } ; globalThis.disableDynamoDBCache = ${
749
+ options ?. disableDynamoDBCache ?? false
750
+ } ;`,
751
+ } ,
720
752
} ) ;
721
753
}
722
754
0 commit comments