|
| 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