Skip to content

Commit 173cefe

Browse files
committed
prep blobs handling to conditionally use frameworks api, but still be able to use either previous setup with regional blobs or just legacy blobs
1 parent dda77b7 commit 173cefe

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

src/build/content/prerendered.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { existsSync } from 'node:fs'
2-
import { mkdir, readFile, writeFile } from 'node:fs/promises'
3-
import { dirname, join } from 'node:path'
2+
import { mkdir, readFile } from 'node:fs/promises'
3+
import { join } from 'node:path'
44

55
import { trace } from '@opentelemetry/api'
66
import { wrapTracer } from '@opentelemetry/api/experimental'
77
import { glob } from 'fast-glob'
88
import pLimit from 'p-limit'
99
import { satisfies } from 'semver'
1010

11-
import { encodeBlobKey } from '../../shared/blobkey.js'
1211
import type {
1312
CachedFetchValue,
1413
NetlifyCachedAppPageValue,
@@ -31,13 +30,11 @@ const writeCacheEntry = async (
3130
lastModified: number,
3231
ctx: PluginContext,
3332
): Promise<void> => {
34-
const path = join(ctx.blobDir, await encodeBlobKey(route), 'blob')
3533
const entry = JSON.stringify({
3634
lastModified,
3735
value,
3836
} satisfies NetlifyCacheHandlerValue)
39-
await mkdir(dirname(path), { recursive: true })
40-
await writeFile(path, entry, 'utf-8')
37+
await ctx.setBlob(route, entry)
4138
}
4239

4340
/**

src/build/content/static.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { existsSync } from 'node:fs'
2-
import { cp, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
3-
import { basename, dirname, join } from 'node:path'
2+
import { cp, mkdir, readFile, rename, rm } from 'node:fs/promises'
3+
import { basename, join } from 'node:path'
44

55
import { trace } from '@opentelemetry/api'
66
import { wrapTracer } from '@opentelemetry/api/experimental'
77
import glob from 'fast-glob'
88

9-
import { encodeBlobKey } from '../../shared/blobkey.js'
109
import { PluginContext } from '../plugin-context.js'
1110
import { verifyNetlifyForms } from '../verification.js'
1211

@@ -33,9 +32,7 @@ export const copyStaticContent = async (ctx: PluginContext): Promise<void> => {
3332
.map(async (path): Promise<void> => {
3433
const html = await readFile(join(srcDir, path), 'utf-8')
3534
verifyNetlifyForms(ctx, html)
36-
const blobPath = join(destDir, await encodeBlobKey(path), 'blob')
37-
await mkdir(dirname(blobPath), { recursive: true })
38-
await writeFile(blobPath, html, 'utf-8')
35+
await ctx.setBlob(path, html)
3936
}),
4037
)
4138
} catch (error) {

src/build/functions/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const getHandlerFile = async (ctx: PluginContext): Promise<string> => {
103103
const templatesDir = join(ctx.pluginDir, 'dist/build/templates')
104104

105105
const templateVariables: Record<string, string> = {
106-
'{{useRegionalBlobs}}': ctx.useRegionalBlobs.toString(),
106+
'{{useRegionalBlobs}}': (ctx.blobsStrategy !== 'legacy').toString(),
107107
'{{generator}}': `${ctx.pluginName}@${ctx.pluginVersion}`,
108108
'{{serverHandlerRootDir}}': ctx.serverHandlerRootDir,
109109
}

src/build/plugin-context.ts

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, readFileSync } from 'node:fs'
2-
import { readFile } from 'node:fs/promises'
2+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
33
import { createRequire } from 'node:module'
4-
import { join, relative, resolve } from 'node:path'
4+
import { dirname, join, relative, resolve } from 'node:path'
55
import { join as posixJoin } from 'node:path/posix'
66
import { fileURLToPath } from 'node:url'
77

@@ -15,6 +15,8 @@ import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middlew
1515
import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
1616
import { satisfies } from 'semver'
1717

18+
import { encodeBlobKey } from '../shared/blobkey.js'
19+
1820
const MODULE_DIR = fileURLToPath(new URL('.', import.meta.url))
1921
const PLUGIN_DIR = join(MODULE_DIR, '../..')
2022
const DEFAULT_PUBLISH_DIR = '.next'
@@ -137,30 +139,62 @@ export class PluginContext {
137139

138140
/**
139141
* Absolute path of the directory that will be deployed to the blob store
142+
* frameworks api: `.netlify/v1/blobs/deploy`
140143
* region aware: `.netlify/deploy/v1/blobs/deploy`
141144
* default: `.netlify/blobs/deploy`
142145
*/
143146
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+
}
148157

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+
}
150173
}
151174

152175
get buildVersion(): string {
153176
return this.constants.NETLIFY_BUILD_VERSION || 'v0.0.0'
154177
}
155178

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+
157189
if (!(this.featureFlags || {})['next-runtime-regional-blobs']) {
158-
return false
190+
return 'legacy'
159191
}
160192

161193
// Region-aware blobs are only available as of CLI v17.23.5 (i.e. Build v29.41.5)
162194
const REQUIRED_BUILD_VERSION = '>=29.41.5'
163195
return satisfies(this.buildVersion, REQUIRED_BUILD_VERSION, { includePrerelease: true })
196+
? 'regional'
197+
: 'legacy'
164198
}
165199

166200
/**

0 commit comments

Comments
 (0)