From 0719213c092753c3987f87dac8a11fb5021cb5d3 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 20 Jan 2022 17:24:23 -0600 Subject: [PATCH] feat: add renderHead util to server --- packages/astro/src/@types/astro.ts | 11 ------ packages/astro/src/core/render/core.ts | 4 +-- packages/astro/src/runtime/server/index.ts | 39 +++++++++------------- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 4434596f84f05..f4e130d38b83e 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -303,17 +303,6 @@ export type Params = Record; export type Props = Record; -export interface RenderPageOptions { - request: { - params?: Params; - url: URL; - canonicalURL: URL; - }; - children: any[]; - props: Props; - css?: string[]; -} - type Body = string; export interface EndpointOutput { diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 554fbfb8a90df..ba52ed573f6f4 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -1,7 +1,7 @@ import type { ComponentInstance, EndpointHandler, MarkdownRenderOptions, Params, Props, Renderer, RouteData, SSRElement } from '../../@types/astro'; import type { LogOptions } from '../logger.js'; -import { renderEndpoint, renderPage } from '../../runtime/server/index.js'; +import { renderEndpoint, renderToString } from '../../runtime/server/index.js'; import { getParams } from '../routing/index.js'; import { createResult } from './result.js'; import { findPathItemByKey, RouteCache, callGetStaticPaths } from './route-cache.js'; @@ -97,7 +97,7 @@ export async function render(opts: RenderOptions): Promise { scripts, }); - let html = await renderPage(result, Component, pageProps, null); + let html = await renderToString(result, Component, pageProps, null); // inject if missing (TODO: is a more robust check needed for comments, etc.?) if (!legacyBuild && !/) { return output; } +// Renders an endpoint request to completion, returning the body. +export async function renderEndpoint(mod: EndpointHandler, params: any) { + const method = 'get'; + const handler = mod[method]; + + if (!handler || typeof handler !== 'function') { + throw new Error(`Endpoint handler not found! Expected an exported function for "${method}"`); + } + + const { body } = await mod.get(params); + + return body; +} + // Calls a component and renders it into a string of HTML export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any) { const Component = await componentFactory(result, props, children); @@ -414,24 +428,10 @@ const uniqueElements = (item: any, index: number, all: any[]) => { return index === all.findIndex((i) => JSON.stringify(i.props) === props && i.children == children); }; -// Renders an endpoint request to completion, returning the body. -export async function renderEndpoint(mod: EndpointHandler, params: any) { - const method = 'get'; - const handler = mod[method]; - - if (!handler || typeof handler !== 'function') { - throw new Error(`Endpoint handler not found! Expected an exported function for "${method}"`); - } - - const { body } = await mod.get(params); - - return body; -} // Renders a page to completion by first calling the factory callback, waiting for its result, and then appending // styles and scripts into the head. -export async function renderPage(result: SSRResult, Component: AstroComponentFactory, props: any, children: any) { - const template = await renderToString(result, Component, props, children); +export async function renderHead(result: SSRResult) { const styles = Array.from(result.styles) .filter(uniqueElements) .map((style) => { @@ -457,17 +457,10 @@ export async function renderPage(result: SSRResult, Component: AstroComponentFac if (needsHydrationStyles) { styles.push(renderElement('style', { props: { 'astro-style': true }, children: 'astro-root, astro-fragment { display: contents; }' })); } - const links = Array.from(result.links) .filter(uniqueElements) .map((link) => renderElement('link', link)); - - // inject styles & scripts at end of - let headPos = template.indexOf(''); - if (headPos === -1) { - return links.join('\n') + styles.join('\n') + scripts.join('\n') + template; // if no , prepend styles & scripts - } - return template.substring(0, headPos) + links.join('\n') + styles.join('\n') + scripts.join('\n') + template.substring(headPos); + return links.join('\n') + styles.join('\n') + scripts.join('\n'); } export async function renderAstroComponent(component: InstanceType) {