chore: simplify fallback rendering (#3262)
This commit is contained in:
@@ -12,17 +12,21 @@ import { SpanStatusCode } from "@opentelemetry/api";
|
||||
import type { ResolvedFreshConfig } from "./config.ts";
|
||||
import type { BuildCache } from "./build_cache.ts";
|
||||
import type { LayoutConfig } from "./types.ts";
|
||||
import { RenderState, setRenderState } from "./runtime/server/preact_hooks.tsx";
|
||||
import { PARTIAL_SEARCH_PARAM } from "./constants.ts";
|
||||
import {
|
||||
FreshScripts,
|
||||
RenderState,
|
||||
setRenderState,
|
||||
} from "./runtime/server/preact_hooks.tsx";
|
||||
import { DEV_ERROR_OVERLAY_URL, PARTIAL_SEARCH_PARAM } from "./constants.ts";
|
||||
import { tracer } from "./otel.ts";
|
||||
import {
|
||||
type ComponentDef,
|
||||
isAsyncAnyComponent,
|
||||
type PageProps,
|
||||
preactRender,
|
||||
renderAsyncAnyComponent,
|
||||
renderRouteComponent,
|
||||
} from "./render.ts";
|
||||
import { renderToString } from "preact-render-to-string";
|
||||
|
||||
export interface Island {
|
||||
file: string;
|
||||
@@ -278,22 +282,47 @@ export class Context<State> {
|
||||
try {
|
||||
setRenderState(state);
|
||||
|
||||
const inner = preactRender(
|
||||
let html = renderToString(
|
||||
vnode ?? h(Fragment, null),
|
||||
this,
|
||||
state,
|
||||
!hasApp,
|
||||
);
|
||||
|
||||
if (!hasApp) {
|
||||
return inner;
|
||||
if (hasApp) {
|
||||
appChild = jsxTemplate([html]);
|
||||
html = renderToString(appVNode);
|
||||
}
|
||||
|
||||
appChild = jsxTemplate([inner]);
|
||||
if (
|
||||
!state.renderedHtmlBody || !state.renderedHtmlHead ||
|
||||
!state.renderedHtmlTag
|
||||
) {
|
||||
let fallback: VNode = jsxTemplate([html]);
|
||||
if (!state.renderedHtmlBody) {
|
||||
let scripts: VNode | null = null;
|
||||
|
||||
const outer = preactRender(appVNode, this, state, true);
|
||||
if (
|
||||
this.url.pathname !== this.config.basePath + DEV_ERROR_OVERLAY_URL
|
||||
) {
|
||||
scripts = h(FreshScripts, null) as VNode;
|
||||
}
|
||||
|
||||
return outer;
|
||||
fallback = h("body", null, fallback, scripts);
|
||||
}
|
||||
if (!state.renderedHtmlHead) {
|
||||
fallback = h(
|
||||
Fragment,
|
||||
null,
|
||||
h("head", null, h("meta", { charset: "utf-8" })),
|
||||
fallback,
|
||||
);
|
||||
}
|
||||
if (!state.renderedHtmlTag) {
|
||||
fallback = h("html", null, fallback);
|
||||
}
|
||||
|
||||
html = renderToString(fallback);
|
||||
}
|
||||
|
||||
return `<!DOCTYPE html>${html}`;
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
span.recordException(err);
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -192,7 +192,7 @@ Deno.test({
|
||||
return (el as any).href;
|
||||
});
|
||||
|
||||
expect(href).toMatch(/\/assets\/client-entry-.*\.css$/);
|
||||
expect(href).toMatch(/\/assets\/client-entry-.*\.css(\?.*)?$/);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user