Skip to content

Commit a42a9b8

Browse files
committed
move static content step to adapter pattern
1 parent 36dc9db commit a42a9b8

File tree

5 files changed

+51
-432
lines changed

5 files changed

+51
-432
lines changed

src/adapter/adapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const adapter: NextAdapter = {
4545
nextAdapterContext,
4646
frameworksAPIConfig,
4747
)
48+
frameworksAPIConfig = await onBuildCompleteForStaticContent(
49+
nextAdapterContext,
50+
frameworksAPIConfig,
51+
)
4852
frameworksAPIConfig = onBuildCompleteForHeaders(nextAdapterContext, frameworksAPIConfig)
4953

5054
if (frameworksAPIConfig) {

src/adapter/static-content.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
2+
import { join } from 'node:path/posix'
3+
4+
import type { HtmlBlob } from '../shared/blob-types.cjs'
5+
import { encodeBlobKey } from '../shared/blobkey.js'
6+
7+
import type { FrameworksAPIConfig, OnBuildCompleteContext } from './types.js'
8+
9+
export async function onBuildComplete(
10+
ctx: OnBuildCompleteContext,
11+
frameworksAPIConfigArg: FrameworksAPIConfig,
12+
) {
13+
const frameworksAPIConfig: FrameworksAPIConfig = frameworksAPIConfigArg ?? {}
14+
15+
const BLOBS_DIRECTORY = join(ctx.projectDir, '.netlify/deploy/v1/blobs/deploy')
16+
17+
try {
18+
await mkdir(BLOBS_DIRECTORY, { recursive: true })
19+
20+
for (const appPage of ctx.outputs.appPages) {
21+
const html = await readFile(appPage.filePath, 'utf-8')
22+
23+
await writeFile(
24+
join(BLOBS_DIRECTORY, await encodeBlobKey(appPage.pathname)),
25+
JSON.stringify({ html, isFullyStaticPage: false } satisfies HtmlBlob),
26+
'utf-8',
27+
)
28+
}
29+
30+
for (const appRoute of ctx.outputs.appRoutes) {
31+
const html = await readFile(appRoute.filePath, 'utf-8')
32+
33+
await writeFile(
34+
join(BLOBS_DIRECTORY, await encodeBlobKey(appRoute.pathname)),
35+
JSON.stringify({ html, isFullyStaticPage: false } satisfies HtmlBlob),
36+
'utf-8',
37+
)
38+
}
39+
} catch (error) {
40+
throw new Error(`Failed assembling static pages for upload`, { cause: error })
41+
}
42+
43+
return frameworksAPIConfig
44+
}

0 commit comments

Comments
 (0)