|
1 | | -import assert from 'node:assert' |
2 | 1 | import fs from 'node:fs' |
3 | 2 | import path from 'node:path' |
4 | 3 | import { Readable } from 'node:stream' |
@@ -60,29 +59,24 @@ async function renderStatic(config: ResolvedConfig) { |
60 | 59 |
|
61 | 60 | // render rsc and html |
62 | 61 | const baseDir = config.environments.client.build.outDir |
63 | | - for (const htmlPath of staticPaths) { |
64 | | - config.logger.info('[vite-rsc:ssg] -> ' + htmlPath) |
65 | | - const rscPath = htmlPath + RSC_POSTFIX |
66 | | - const htmlResponse = await entry.default( |
67 | | - new Request(new URL(htmlPath, 'http://ssg.local')), |
| 62 | + for (const staticPatch of staticPaths) { |
| 63 | + config.logger.info('[vite-rsc:ssg] -> ' + staticPatch) |
| 64 | + const { html, rsc } = await entry.handleSsg( |
| 65 | + new Request(new URL(staticPatch, 'http://ssg.local')), |
68 | 66 | ) |
69 | | - assert.equal(htmlResponse.status, 200) |
70 | | - await fs.promises.writeFile( |
71 | | - path.join(baseDir, normalizeHtmlFilePath(htmlPath)), |
72 | | - Readable.fromWeb(htmlResponse.body as any), |
73 | | - ) |
74 | | - |
75 | | - const rscResponse = await entry.default( |
76 | | - new Request(new URL(rscPath, 'http://ssg.local')), |
77 | | - ) |
78 | | - assert.equal(rscResponse.status, 200) |
79 | | - await fs.promises.writeFile( |
80 | | - path.join(baseDir, rscPath), |
81 | | - Readable.fromWeb(rscResponse.body as any), |
| 67 | + await writeFileStream( |
| 68 | + path.join(baseDir, normalizeHtmlFilePath(staticPatch)), |
| 69 | + html, |
82 | 70 | ) |
| 71 | + await writeFileStream(path.join(baseDir, staticPatch + RSC_POSTFIX), rsc) |
83 | 72 | } |
84 | 73 | } |
85 | 74 |
|
| 75 | +async function writeFileStream(filePath: string, stream: ReadableStream) { |
| 76 | + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }) |
| 77 | + await fs.promises.writeFile(filePath, Readable.fromWeb(stream as any)) |
| 78 | +} |
| 79 | + |
86 | 80 | function normalizeHtmlFilePath(p: string) { |
87 | 81 | if (p.endsWith('/')) { |
88 | 82 | return p + 'index.html' |
|
0 commit comments