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 { ResolvedFreshConfig } from "./config.ts";
|
||||||
import type { BuildCache } from "./build_cache.ts";
|
import type { BuildCache } from "./build_cache.ts";
|
||||||
import type { LayoutConfig } from "./types.ts";
|
import type { LayoutConfig } from "./types.ts";
|
||||||
import { RenderState, setRenderState } from "./runtime/server/preact_hooks.tsx";
|
import {
|
||||||
import { PARTIAL_SEARCH_PARAM } from "./constants.ts";
|
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 { tracer } from "./otel.ts";
|
||||||
import {
|
import {
|
||||||
type ComponentDef,
|
type ComponentDef,
|
||||||
isAsyncAnyComponent,
|
isAsyncAnyComponent,
|
||||||
type PageProps,
|
type PageProps,
|
||||||
preactRender,
|
|
||||||
renderAsyncAnyComponent,
|
renderAsyncAnyComponent,
|
||||||
renderRouteComponent,
|
renderRouteComponent,
|
||||||
} from "./render.ts";
|
} from "./render.ts";
|
||||||
|
import { renderToString } from "preact-render-to-string";
|
||||||
|
|
||||||
export interface Island {
|
export interface Island {
|
||||||
file: string;
|
file: string;
|
||||||
@@ -278,22 +282,47 @@ export class Context<State> {
|
|||||||
try {
|
try {
|
||||||
setRenderState(state);
|
setRenderState(state);
|
||||||
|
|
||||||
const inner = preactRender(
|
let html = renderToString(
|
||||||
vnode ?? h(Fragment, null),
|
vnode ?? h(Fragment, null),
|
||||||
this,
|
|
||||||
state,
|
|
||||||
!hasApp,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!hasApp) {
|
if (hasApp) {
|
||||||
return inner;
|
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) {
|
} catch (err) {
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
span.recordException(err);
|
span.recordException(err);
|
||||||
|
|||||||
@@ -5,15 +5,8 @@ import {
|
|||||||
type RenderableProps,
|
type RenderableProps,
|
||||||
type VNode,
|
type VNode,
|
||||||
} from "preact";
|
} from "preact";
|
||||||
import {
|
|
||||||
FreshScripts,
|
|
||||||
type RenderState,
|
|
||||||
} from "./runtime/server/preact_hooks.tsx";
|
|
||||||
import type { Context } from "./context.ts";
|
import type { Context } from "./context.ts";
|
||||||
import { recordSpanError, tracer } from "./otel.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> = {
|
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> =
|
export type PageProps<Data = unknown, T = unknown> =
|
||||||
& Pick<
|
& Pick<
|
||||||
Context<T>,
|
Context<T>,
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ Deno.test({
|
|||||||
return (el as any).href;
|
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