Skip to content

Commit ae8672e

Browse files
committed
chore: attempt to improve hdyration
1 parent 0e07e21 commit ae8672e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/generators/web/utils/generate.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ export default () => {
8181
...baseImports,
8282

8383
// Import Preact's SSR render function (named import)
84-
createImportDeclaration('render', 'preact-render-to-string', false),
84+
createImportDeclaration(
85+
'renderToString',
86+
'preact-render-to-string',
87+
false
88+
),
8589

8690
// Render component to HTML string and return it
87-
`return render(${componentCode});`,
91+
`return renderToString(${componentCode});`,
8892
].join('\n');
8993
};
9094

91-
return {
92-
buildClientProgram,
93-
buildServerProgram,
94-
};
95+
return { buildClientProgram, buildServerProgram };
9596
};

src/generators/web/utils/processing.mjs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
22

33
import HTMLMinifier from '@minify-html/node';
44
import { jsx, toJs } from 'estree-util-to-js';
5+
import { transform } from 'lightningcss';
56

67
import { SPECULATION_RULES } from '../constants.mjs';
78
import bundleCode from './bundle.mjs';
@@ -48,13 +49,13 @@ export function convertJSXToCode(
4849
*
4950
* @param {Map<string, string>} serverCodeMap - Map of fileName to server-side JavaScript code.
5051
* @param {ReturnType<import('node:module').createRequire>} requireFn - Node.js require function for external packages.
51-
* @returns {Promise<Map<string, string>>} Map of fileName to dehydrated (server-rendered) HTML content.
52+
* @returns {{ pages: Map<string, string>, css: string }} Map of fileName to dehydrated (server-rendered) HTML content.
5253
*/
5354
export async function executeServerCode(serverCodeMap, requireFn) {
5455
const dehydratedMap = new Map();
5556

5657
// Bundle all server-side code, which may produce code-split chunks
57-
const { chunks } = await bundleCode(serverCodeMap, { server: true });
58+
const { chunks, css } = await bundleCode(serverCodeMap, { server: true });
5859

5960
const entryChunks = chunks.filter(c => c.isEntry);
6061
const otherChunks = chunks.filter(c => !c.isEntry);
@@ -71,7 +72,7 @@ export async function executeServerCode(serverCodeMap, requireFn) {
7172
dehydratedMap.set(chunk.fileName, executedFunction(enhancedRequire));
7273
}
7374

74-
return dehydratedMap;
75+
return { pages: dehydratedMap, css };
7576
}
7677

7778
/**
@@ -115,7 +116,7 @@ export async function processJSXEntries(
115116
// Replace template placeholders with actual content
116117
const renderedHtml = template
117118
.replace('{{title}}', `${heading.data.name} | ${titleSuffix}`)
118-
.replace('{{dehydrated}}', serverBundle.get(fileName) ?? '')
119+
.replace('{{dehydrated}}', serverBundle.pages.get(fileName) ?? '')
119120
.replace('{{importMap}}', clientBundle.importMap ?? '')
120121
.replace('{{entrypoint}}', `./${fileName}?${randomUUID()}`)
121122
.replace('{{speculationRules}}', speculationRulesString);
@@ -126,5 +127,10 @@ export async function processJSXEntries(
126127
return { html: finalHTMLBuffer, api };
127128
});
128129

129-
return { results, ...clientBundle };
130+
const { code: minifiedCSS } = transform({
131+
code: Buffer.from(`${serverBundle.css}\n${clientBundle.css}`),
132+
minify: true,
133+
});
134+
135+
return { results, chunks: clientBundle.chunks, css: minifiedCSS };
130136
}

0 commit comments

Comments
 (0)