chore: simplify fallback rendering (#3262)

This commit is contained in:
Marvin Hagemeister
2025-08-26 14:38:44 +02:00
committed by GitHub
parent 671af16a82
commit 7afae637fe
3 changed files with 42 additions and 60 deletions
-47
View File
@@ -5,15 +5,8 @@ import {
type RenderableProps,
type VNode,
} from "preact";
import {
FreshScripts,
type RenderState,
} from "./runtime/server/preact_hooks.tsx";
import type { Context } from "./context.ts";
import { recordSpanError, tracer } from "./otel.ts";
import { DEV_ERROR_OVERLAY_URL } from "./constants.ts";
import { renderToString } from "preact-render-to-string";
import { escape as escapeHtml } from "@std/html";
export type AsyncAnyComponent<P> = {
(
@@ -56,46 +49,6 @@ export async function renderAsyncAnyComponent<Props>(
);
}
export function preactRender<State, Data>(
vnode: VNode,
ctx: PageProps<Data, State>,
state: RenderState,
renderFallback: boolean,
) {
let res = renderToString(vnode);
if (!renderFallback) return res;
// We require a the full outer DOM structure so that browser put
// comment markers in the right place in the DOM.
if (!state.renderedHtmlBody) {
let scripts = "";
if (ctx.url.pathname !== ctx.config.basePath + DEV_ERROR_OVERLAY_URL) {
scripts = renderToString(h(FreshScripts, null));
}
res = `<body>${res}${scripts}</body>`;
}
if (!state.renderedHtmlHead) {
let head = `<head><meta charset="utf-8">`;
const entryAssets = state.buildCache.getEntryAssets();
for (let i = 0; i < entryAssets.length; i++) {
const asset = entryAssets[i];
if (asset.endsWith(".css")) {
head += `<link rel="stylesheet" href="${escapeHtml(asset)}">`;
}
}
res = `${head}</head>${res}`;
}
if (!state.renderedHtmlTag) {
res = `<html>${res}</html>`;
}
return `<!DOCTYPE html>${res}`;
}
export type PageProps<Data = unknown, T = unknown> =
& Pick<
Context<T>,