Skip to content

Commit 9044dce

Browse files
committed
feat: include critical css into html template
1 parent 2992ba1 commit 9044dce

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

examples/basic/src/entry-server.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function ServerEntry({ children }: ServerEntryProps) {
3434
/>
3535
<Meta />
3636
</head>
37-
<body>
37+
<body class="dark">
3838
{children}
3939
<Scripts />
4040
</body>

packages/pranx/src/build/generate_html_template.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import { META_TAG } from "@/server/components/meta";
22
import { SCRIPTS_TAG } from "@/server/components/scripts";
33
import { minifySync } from "@swc/html";
4+
import { readFileSync } from "node:fs";
5+
6+
let critical_css = "";
47

58
export const generate_html_template = ({
69
hydrate_data_as_string,
710
page_prerendered,
811
minify = false,
9-
css = [],
12+
css_links = [],
13+
critical_css_filepath = "",
1014
}: {
1115
page_prerendered: string;
1216
hydrate_data_as_string: string;
1317
minify: boolean;
14-
css: string[];
18+
css_links: string[];
19+
critical_css_filepath: string;
1520
}) => {
21+
if (!critical_css) {
22+
critical_css = readFileSync(critical_css_filepath, { encoding: "utf8" });
23+
}
24+
1625
const template = `
1726
<!DOCTYPE html>
1827
${page_prerendered
1928
.replace(
2029
META_TAG,
21-
`${css.map((css_path) => `<link rel="stylesheet" href="${css_path}" />`).join("\n")}`
30+
`<style>${critical_css}</style>
31+
${css_links.map((css_path) => `<link rel="stylesheet" href="${css_path}" />`).join("\n")}`
2232
)
2333
.replace(
2434
SCRIPTS_TAG,
25-
`
26-
<script>window.__PRANX_HYDRATE_DATA__=${hydrate_data_as_string}</script>
27-
<script type="module" src="/_.._/entry-client.js"></script>`
35+
`<script>window.__PRANX_HYDRATE_DATA__=${hydrate_data_as_string}</script>
36+
<script type="module" src="/_.._/entry-client.js"></script>`
2837
)}`;
2938

3039
if (!minify) return template;

packages/pranx/src/cmd/build.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function build() {
3838
await fse.emptyDir(OUTPUT_PRANX_DIR);
3939

4040
// Bundling
41-
const optimize_output = true;
41+
const optimize_output = false;
4242

4343
const server_bundle_result = await bundle_server({
4444
optimize: optimize_output,
@@ -53,6 +53,7 @@ export async function build() {
5353
// Manifests
5454
const server_site_manifest: SERVER_MANIFEST = {
5555
entry_server: join(OUTPUT_BUNDLE_SERVER_DIR, "entry-server.js"),
56+
global_css_filepath: "",
5657
routes: [],
5758
api: [],
5859
};
@@ -101,7 +102,7 @@ export async function build() {
101102
if (!file.endsWith(".css")) continue;
102103

103104
if (file.endsWith("entry-client.css")) {
104-
css_output.entry = file.replace(pranx_browser_base_path, "");
105+
css_output.entry = join(OUTPUT_BUNDLE_BROWSER_DIR, file.replace(pranx_browser_base_path, ""));
105106
continue;
106107
}
107108

@@ -116,6 +117,8 @@ export async function build() {
116117
css_output[path_normalized] = css_file_relative;
117118
}
118119

120+
server_site_manifest.global_css_filepath = css_output.entry;
121+
119122
// Generating Manifest and generating static pages data
120123
for (const [file, _output] of Object.entries(browser_bundle_result.metafile.outputs)) {
121124
if (!file.endsWith("page.js")) continue;
@@ -189,7 +192,7 @@ export async function build() {
189192
revalidate: statics_fn_result.revalidate || -1,
190193
is_dynamic: isUrlDynamic,
191194
dynamic_params: dynamic_params,
192-
css: [css_output.entry, css_output[final_path_normalized] || ""].filter(Boolean),
195+
css: [css_output[final_path_normalized] || ""].filter(Boolean),
193196
static_generated_routes: [],
194197
absolute_module_path: module_path,
195198
});
@@ -247,12 +250,13 @@ export async function build() {
247250
static_generated_routes: [],
248251
is_dynamic: isUrlDynamic,
249252
dynamic_params: dynamic_params,
250-
css: [css_output.entry, css_output[final_path_normalized] || ""].filter(Boolean) as string[],
253+
css: [css_output[final_path_normalized] || ""].filter(Boolean) as string[],
251254
absolute_module_path: module_path,
252255
});
253256
}
254257

255258
const hydrate_data: HYDRATE_DATA = {
259+
entry_css: css_output.entry,
256260
routes: server_site_manifest.routes.map((r) => {
257261
return {
258262
module: r.module,
@@ -294,7 +298,8 @@ export async function build() {
294298
page_prerendered,
295299
hydrate_data_as_string,
296300
minify: optimize_output,
297-
css: route.css,
301+
css_links: route.css,
302+
critical_css_filepath: server_site_manifest.global_css_filepath,
298303
});
299304

300305
const output_html_path = join(OUTPUT_BUNDLE_BROWSER_DIR, static_route.path, "index.html");
@@ -314,7 +319,8 @@ export async function build() {
314319
page_prerendered,
315320
hydrate_data_as_string,
316321
minify: true,
317-
css: route.css,
322+
css_links: route.css,
323+
critical_css_filepath: server_site_manifest.global_css_filepath,
318324
});
319325

320326
const output_html_path = join(OUTPUT_BUNDLE_BROWSER_DIR, route.path, "index.html");

packages/pranx/src/server/runtime/define-ssr-handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export const define_ssr_handlers = async (server_manifest: SERVER_MANIFEST, app:
6666
page_prerendered,
6767
hydrate_data_as_string: JSON.stringify(hydrate_data),
6868
minify: true,
69-
css: route.css,
69+
css_links: route.css,
70+
critical_css_filepath: server_manifest.global_css_filepath,
7071
});
7172

7273
return html(event, html_string);

packages/pranx/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export type ServerManifestApiHandler = {
9393

9494
export type SERVER_MANIFEST = {
9595
entry_server: string;
96+
global_css_filepath: string;
9697
routes: ServerManifestRoute[];
9798
api: ServerManifestApiHandler[];
9899
};
@@ -110,6 +111,7 @@ export type HydrateDataRoute = {
110111

111112
export type HYDRATE_DATA = {
112113
routes: HydrateDataRoute[];
114+
entry_css: string;
113115
};
114116

115117
declare global {

0 commit comments

Comments
 (0)