1
1
import { existsSync , readFileSync } from 'node:fs'
2
- import { readFile } from 'node:fs/promises'
2
+ import { mkdir , readFile , writeFile } from 'node:fs/promises'
3
3
import { createRequire } from 'node:module'
4
- import { join , relative , resolve } from 'node:path'
4
+ import { dirname , join , relative , resolve } from 'node:path'
5
5
import { join as posixJoin } from 'node:path/posix'
6
6
import { fileURLToPath } from 'node:url'
7
7
@@ -15,6 +15,8 @@ import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middlew
15
15
import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
16
16
import { satisfies } from 'semver'
17
17
18
+ import { encodeBlobKey } from '../shared/blobkey.js'
19
+
18
20
const MODULE_DIR = fileURLToPath ( new URL ( '.' , import . meta. url ) )
19
21
const PLUGIN_DIR = join ( MODULE_DIR , '../..' )
20
22
const DEFAULT_PUBLISH_DIR = '.next'
@@ -137,30 +139,62 @@ export class PluginContext {
137
139
138
140
/**
139
141
* Absolute path of the directory that will be deployed to the blob store
142
+ * frameworks api: `.netlify/v1/blobs/deploy`
140
143
* region aware: `.netlify/deploy/v1/blobs/deploy`
141
144
* default: `.netlify/blobs/deploy`
142
145
*/
143
146
get blobDir ( ) : string {
144
- return this . resolveFromPackagePath ( '.netlify/v1/blobs/deploy' )
145
- // if (this.useRegionalBlobs) {
146
- // return this.resolveFromPackagePath('.netlify/deploy/v1/blobs/deploy')
147
- // }
147
+ switch ( this . blobsStrategy ) {
148
+ case 'frameworks-api' :
149
+ return this . resolveFromPackagePath ( '.netlify/v1/blobs/deploy' )
150
+ case 'regional' :
151
+ return this . resolveFromPackagePath ( '.netlify/deploy/v1/blobs/deploy' )
152
+ case 'legacy' :
153
+ default :
154
+ return this . resolveFromPackagePath ( '.netlify/blobs/deploy' )
155
+ }
156
+ }
148
157
149
- // return this.resolveFromPackagePath('.netlify/blobs/deploy')
158
+ async setBlob ( key : string , value : string ) {
159
+ switch ( this . blobsStrategy ) {
160
+ case 'frameworks-api' : {
161
+ const path = join ( this . blobDir , await encodeBlobKey ( key ) , 'blob' )
162
+ await mkdir ( dirname ( path ) , { recursive : true } )
163
+ await writeFile ( path , value , 'utf-8' )
164
+ return
165
+ }
166
+ case 'regional' :
167
+ case 'legacy' :
168
+ default : {
169
+ const path = join ( this . blobDir , await encodeBlobKey ( key ) )
170
+ await writeFile ( path , value , 'utf-8' )
171
+ }
172
+ }
150
173
}
151
174
152
175
get buildVersion ( ) : string {
153
176
return this . constants . NETLIFY_BUILD_VERSION || 'v0.0.0'
154
177
}
155
178
156
- get useRegionalBlobs ( ) : boolean {
179
+ get useFrameworksAPI ( ) : boolean {
180
+ // TODO: make this conditional
181
+ return true
182
+ }
183
+
184
+ get blobsStrategy ( ) : 'legacy' | 'regional' | 'frameworks-api' {
185
+ if ( this . useFrameworksAPI ) {
186
+ return 'frameworks-api'
187
+ }
188
+
157
189
if ( ! ( this . featureFlags || { } ) [ 'next-runtime-regional-blobs' ] ) {
158
- return false
190
+ return 'legacy'
159
191
}
160
192
161
193
// Region-aware blobs are only available as of CLI v17.23.5 (i.e. Build v29.41.5)
162
194
const REQUIRED_BUILD_VERSION = '>=29.41.5'
163
195
return satisfies ( this . buildVersion , REQUIRED_BUILD_VERSION , { includePrerelease : true } )
196
+ ? 'regional'
197
+ : 'legacy'
164
198
}
165
199
166
200
/**
0 commit comments