@@ -59,6 +59,86 @@ function getCacheAssetPaths(opts: BuildOptions) {
5959 } ) ;
6060}
6161
62+ function populateR2IncrementalCache (
63+ options : BuildOptions ,
64+ populateCacheOptions : { target : WranglerTarget ; environment ?: string }
65+ ) {
66+ const config = unstable_readConfig ( { env : populateCacheOptions . environment } ) ;
67+
68+ const binding = ( config . r2_buckets ?? [ ] ) . find ( ( { binding } ) => binding === R2_CACHE_BINDING_NAME ) ;
69+
70+ if ( ! binding ) {
71+ throw new Error ( `No R2 binding ${ JSON . stringify ( R2_CACHE_BINDING_NAME ) } found!` ) ;
72+ }
73+
74+ const bucket = binding . bucket_name ;
75+
76+ if ( ! bucket ) {
77+ throw new Error ( `R2 binding ${ JSON . stringify ( R2_CACHE_BINDING_NAME ) } should have a 'bucket_name'` ) ;
78+ }
79+
80+ logger . info ( "\nPopulating R2 incremental cache..." ) ;
81+
82+ const assets = getCacheAssetPaths ( options ) ;
83+ for ( const { fsPath, destPath } of tqdm ( assets ) ) {
84+ const fullDestPath = path . join (
85+ bucket ,
86+ process . env [ R2_CACHE_PREFIX_ENV_NAME ] ?? R2_CACHE_DEFAULT_PREFIX ,
87+ destPath
88+ ) ;
89+
90+ runWrangler (
91+ options ,
92+ [ "r2 object put" , JSON . stringify ( fullDestPath ) , `--file ${ JSON . stringify ( fsPath ) } ` ] ,
93+ // NOTE: R2 does not support the environment flag and results in the following error:
94+ // Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
95+ { target : populateCacheOptions . target , excludeRemoteFlag : true , logging : "error" }
96+ ) ;
97+ }
98+ logger . info ( `Successfully populated cache with ${ assets . length } assets` ) ;
99+ }
100+
101+ function populateKVIncrementalCache (
102+ options : BuildOptions ,
103+ populateCacheOptions : { target : WranglerTarget ; environment ?: string }
104+ ) {
105+ logger . info ( "\nPopulating KV incremental cache..." ) ;
106+
107+ const assets = getCacheAssetPaths ( options ) ;
108+ for ( const { fsPath, destPath } of tqdm ( assets ) ) {
109+ runWrangler (
110+ options ,
111+ [
112+ "kv key put" ,
113+ JSON . stringify ( destPath ) ,
114+ `--binding ${ JSON . stringify ( KV_CACHE_BINDING_NAME ) } ` ,
115+ `--path ${ JSON . stringify ( fsPath ) } ` ,
116+ ] ,
117+ { ...populateCacheOptions , logging : "error" }
118+ ) ;
119+ }
120+ logger . info ( `Successfully populated cache with ${ assets . length } assets` ) ;
121+ }
122+
123+ function populateD1TagCache (
124+ options : BuildOptions ,
125+ populateCacheOptions : { target : WranglerTarget ; environment ?: string }
126+ ) {
127+ logger . info ( "\nCreating D1 table if necessary..." ) ;
128+
129+ runWrangler (
130+ options ,
131+ [
132+ "d1 execute" ,
133+ JSON . stringify ( D1_TAG_BINDING_NAME ) ,
134+ `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"` ,
135+ ] ,
136+ { ...populateCacheOptions , logging : "error" }
137+ ) ;
138+
139+ logger . info ( "\nSuccessfully created D1 table" ) ;
140+ }
141+
62142export async function populateCache (
63143 options : BuildOptions ,
64144 config : OpenNextConfig ,
@@ -74,61 +154,10 @@ export async function populateCache(
74154 if ( ! config . dangerous ?. disableIncrementalCache && incrementalCache ) {
75155 const name = await resolveCacheName ( incrementalCache ) ;
76156 switch ( name ) {
77- case R2_CACHE_NAME : {
78- const config = unstable_readConfig ( { env : populateCacheOptions . environment } ) ;
79-
80- const binding = ( config . r2_buckets ?? [ ] ) . find ( ( { binding } ) => binding === R2_CACHE_BINDING_NAME ) ;
81-
82- if ( ! binding ) {
83- throw new Error ( `No R2 binding ${ JSON . stringify ( R2_CACHE_BINDING_NAME ) } found!` ) ;
84- }
85-
86- const bucket = binding . bucket_name ;
87-
88- if ( ! bucket ) {
89- throw new Error ( `R2 binding ${ JSON . stringify ( R2_CACHE_BINDING_NAME ) } should have a 'bucket_name'` ) ;
90- }
91-
92- logger . info ( "\nPopulating R2 incremental cache..." ) ;
93-
94- const assets = getCacheAssetPaths ( options ) ;
95- for ( const { fsPath, destPath } of tqdm ( assets ) ) {
96- const fullDestPath = path . join (
97- bucket ,
98- process . env [ R2_CACHE_PREFIX_ENV_NAME ] ?? R2_CACHE_DEFAULT_PREFIX ,
99- destPath
100- ) ;
101-
102- runWrangler (
103- options ,
104- [ "r2 object put" , JSON . stringify ( fullDestPath ) , `--file ${ JSON . stringify ( fsPath ) } ` ] ,
105- // NOTE: R2 does not support the environment flag and results in the following error:
106- // Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
107- { target : populateCacheOptions . target , excludeRemoteFlag : true , logging : "error" }
108- ) ;
109- }
110- logger . info ( `Successfully populated cache with ${ assets . length } assets` ) ;
111- break ;
112- }
113- case KV_CACHE_NAME : {
114- logger . info ( "\nPopulating KV incremental cache..." ) ;
115-
116- const assets = getCacheAssetPaths ( options ) ;
117- for ( const { fsPath, destPath } of tqdm ( assets ) ) {
118- runWrangler (
119- options ,
120- [
121- "kv key put" ,
122- JSON . stringify ( destPath ) ,
123- `--binding ${ JSON . stringify ( KV_CACHE_BINDING_NAME ) } ` ,
124- `--path ${ JSON . stringify ( fsPath ) } ` ,
125- ] ,
126- { ...populateCacheOptions , logging : "error" }
127- ) ;
128- }
129- logger . info ( `Successfully populated cache with ${ assets . length } assets` ) ;
130- break ;
131- }
157+ case R2_CACHE_NAME :
158+ return populateR2IncrementalCache ( options , populateCacheOptions ) ;
159+ case KV_CACHE_NAME :
160+ return populateKVIncrementalCache ( options , populateCacheOptions ) ;
132161 default :
133162 logger . info ( "Incremental cache does not need populating" ) ;
134163 }
@@ -137,22 +166,8 @@ export async function populateCache(
137166 if ( ! config . dangerous ?. disableTagCache && ! config . dangerous ?. disableIncrementalCache && tagCache ) {
138167 const name = await resolveCacheName ( tagCache ) ;
139168 switch ( name ) {
140- case D1_TAG_NAME : {
141- logger . info ( "\nCreating D1 table if necessary..." ) ;
142-
143- runWrangler (
144- options ,
145- [
146- "d1 execute" ,
147- JSON . stringify ( D1_TAG_BINDING_NAME ) ,
148- `--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"` ,
149- ] ,
150- { ...populateCacheOptions , logging : "error" }
151- ) ;
152-
153- logger . info ( "\nSuccessfully created D1 table" ) ;
154- break ;
155- }
169+ case D1_TAG_NAME :
170+ return populateD1TagCache ( options , populateCacheOptions ) ;
156171 default :
157172 logger . info ( "Tag cache does not need populating" ) ;
158173 }
0 commit comments