63 lines
2.0 KiB
Plaintext
63 lines
2.0 KiB
Plaintext
import { renderComponentToString } from "./component.js";
|
|
import { isAstroComponentFactory } from "./astro/index.js";
|
|
import { renderToReadableStream, renderToString, renderToAsyncIterable } from "./astro/render.js";
|
|
import { encoder } from "./common.js";
|
|
import { isNode } from "./util.js";
|
|
async function renderPage(result, componentFactory, props, children, streaming, route) {
|
|
if (!isAstroComponentFactory(componentFactory)) {
|
|
result._metadata.headInTree = result.componentMetadata.get(componentFactory.moduleId)?.containsHead ?? false;
|
|
const pageProps = { ...props ?? {}, "server:root": true };
|
|
const str = await renderComponentToString(
|
|
result,
|
|
componentFactory.name,
|
|
componentFactory,
|
|
pageProps,
|
|
{},
|
|
true,
|
|
route
|
|
);
|
|
const bytes = encoder.encode(str);
|
|
return new Response(bytes, {
|
|
headers: new Headers([
|
|
["Content-Type", "text/html; charset=utf-8"],
|
|
["Content-Length", bytes.byteLength.toString()]
|
|
])
|
|
});
|
|
}
|
|
result._metadata.headInTree = result.componentMetadata.get(componentFactory.moduleId)?.containsHead ?? false;
|
|
let body;
|
|
if (streaming) {
|
|
if (isNode) {
|
|
const nodeBody = await renderToAsyncIterable(
|
|
result,
|
|
componentFactory,
|
|
props,
|
|
children,
|
|
true,
|
|
route
|
|
);
|
|
body = nodeBody;
|
|
} else {
|
|
body = await renderToReadableStream(result, componentFactory, props, children, true, route);
|
|
}
|
|
} else {
|
|
body = await renderToString(result, componentFactory, props, children, true, route);
|
|
}
|
|
if (body instanceof Response)
|
|
return body;
|
|
const init = result.response;
|
|
const headers = new Headers(init.headers);
|
|
if (!streaming && typeof body === "string") {
|
|
body = encoder.encode(body);
|
|
headers.set("Content-Length", body.byteLength.toString());
|
|
}
|
|
if (route?.component.endsWith(".md")) {
|
|
headers.set("Content-Type", "text/html; charset=utf-8");
|
|
}
|
|
const response = new Response(body, { ...init, headers });
|
|
return response;
|
|
}
|
|
export {
|
|
renderPage
|
|
};
|